I’m pulling date values out of mysql and displaying them in locally used format. See below:
I have this datetime value 2012-11-01 08:00:00 … yyyy-MM-dd HH:mm:ss
I need this to appear as 01-11-2012 08:00:00 … dd-MM-yyyy HH:mm:ss
I can do this simply enough as follows:
Dim dr As MySqlDataReader = cmd.ExecuteReader(sql)
If dr.Read()
Dim mydate As DateTime = dr("date_column")
txtDate.Text = mydate.ToString("dd-MM-yyyy") & " " & mydate.ToString("HH:mm:ss")
End If
This works well enough, but my problem is that if I want to update the record from this textbox, I can’t use this format for the date because mysql complains about an error (object out of range or something).
So what I need to do now is to convert this back to yyyy-MM-dd. The difficulty here is that .NET will automatically assume my dd value should be the MM value and vice versa.
This results in a value of 01-11-2012 writing into the database as 11-01-2012.
How can I reformat the date for insert/update into the database while maintaining the integrity of the date that’s being used here?
Thanks in advance
Use a parameterized query for the update:
If that doesn’t parse the date from the TextBox correct, parse it beforehand with:
Also, the line:
could also be written as:
which might be easier to read.