Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I'm using this code:

string date = eventStartDate.Text;
            string hour = eventStartHour.Text;
            string min = eventStartMinute.Text;

            //Set datestring and the format
            string dateString = date + " " + hour + ":" + min;
            string[] format = new string[] { "d/M/yyyy h:mm", "d/M/yyyy h:mm:ss", "d-M-yyyy h:mm", "dd-MM-yyyy hh:mm" };

            DateTime dateTime;
            DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
            Console.WriteLine(dateTime);


and it's working but just not giving the format I want it to... I want something along the lines of:

"dd/MM/yyyy hh:mm" or "dd-MM-yyyy hh:mm" Which CultureInfo should I use for that?

I'm doing this to pass them to the DB:

protected void submit_Click(object sender, EventArgs e)
        { 
            DateTime eventStart = dates(eventStartDate.Text, eventStartHour.Text, eventStartMinute.Text);
            DateTime departure = dates(departureDate.Text, departureHour.Text, departureMinute.Text);
            DateTime arrival = dates(arrivalDate.Text, arrivalHour.Text, arrivalMinute.Text);
            Console.WriteLine(arrival);
            DateTime flightDeparture = dates(flightDepartureStartDate.Text, flightDepartureStartHour.Text, flightDepartureStartMinute.Text);
            DateTime flightReturn = dates(flightReturnStartDate.Text, flightReturnStartHour.Text, flightReturnStartMinute.Text);

            byte[] file = (byte[])Session["File"];
            string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
            string ConStr = ConfigurationManager.ConnectionStrings["FORgestIT"].ToString();

            SqlConnection SQLConn = new SqlConnection(ConStr); //The SQL Connection              

            SqlCommand SQLCmd = new SqlCommand();
            SQLCmd.Connection = SQLConn;
            SQLCmd.CommandType = CommandType.Text;

            SQLCmd.CommandText = "INSERT INTO Event (Name, Project, Objectives, City, Country, Event_Start, Departure, Arrival, Registration, National_Transportation, Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight, FL_departure, FL_Depart_Prefer, FL_Depart_URL, FL_Return, FL_Ret_Prefer, FL_RET_URL, Notes, File, Status) VALUES (@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)";

            SQLCmd.Parameters.Clear();
            SQLCmd.Parameters.AddWithValue("@Name", name.Text);
            SQLCmd.Parameters.AddWithValue("@Project", project.Text);
            SQLCmd.Parameters.AddWithValue("@Objectives", objectives.Text);
            SQLCmd.Parameters.AddWithValue("@City", venueCity.Text);
            SQLCmd.Parameters.AddWithValue("@Country", venueCountry.Text);
            SQLCmd.Parameters.AddWithValue("@Event_Start", eventStart);
            SQLCmd.Parameters.AddWithValue("@Departure", departure);
            SQLCmd.Parameters.AddWithValue("@Arrival", arrival);
            SQLCmd.Parameters.AddWithValue("@Registration", registrationInformation.Text);
            SQLCmd.Parameters.AddWithValue("@National_Transportation", rdlYesNo.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@Accomodation", accomodation.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@AC_NumberNights", numberOfNights.Text);
            SQLCmd.Parameters.AddWithValue("@AC_PreferHotel", preferredHotel.Text);
            SQLCmd.Parameters.AddWithValue("@AC_PreferHotelURL", preferredHotelURL.Text);
            SQLCmd.Parameters.AddWithValue("@Flight", flight.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@FL_Departure", flightDeparture);
            SQLCmd.Parameters.AddWithValue("@FL_Depart_Prefer", flightDeparturePreferred.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Depart_URL", flightDeparturePreferredURL.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Return", flightReturn);
            SQLCmd.Parameters.AddWithValue("@FL_Ret_Prefer", flightReturnPreferred.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Ret_URL", flightReturnPreferredURL.Text);
            SQLCmd.Parameters.AddWithValue("@Notes", notes.Text);
            SQLCmd.Parameters.AddWithValue("@File", file);
            SQLCmd.Parameters.AddWithValue("@Status", "Pending");

            if (SQLConn.State == ConnectionState.Closed)
            {
                SQLConn.Open();
            }

            SQLCmd.ExecuteNonQuery();
            SQLConn.Close();

            string url = "Default.aspx";
            ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Your request form was saved correctly!');window.location.href = '" + url + "';", true);
        }
Posted
Updated 27-Jan-15 0:50am
v2

1 solution

DateTime is a binary format, which means that it stores the value as a number without any formatting...
When you pass dateTime to WriteLine you actually run ToString method of DateTime on it, but without any formatting (that measn it will use the current culture)...
If you want to get a specific formatting you should pass the format string as parameter, like this:
C#
string format = "dd/MM/yyyy hh:mm";
Console.WriteLine(dateTime.ToString(format));

https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx[^]
 
Share this answer
 
Comments
varmartins 27-Jan-15 5:14am    
What's happening is that I'm trying to save a date with the format dd-MM-yyyy hh:mm:ss to DB and it says the required format is dd/MM/yyyy hh:mm:ss how to fix this?
Kornfeld Eliyahu Peter 27-Jan-15 5:20am    
Date/time values are binary! How do you pass it to the DB?
varmartins 27-Jan-15 6:51am    
I updated the question
Kornfeld Eliyahu Peter 27-Jan-15 6:54am    
Now you have a duplicate...:-) I will left this alone and try to help you on the other (simple there are more people involved there, so you have a better chance to get answer...
TheRealSteveJudge 27-Jan-15 10:06am    
Good solution. 5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900