Formatting DateTime
-
Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh:
DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy"));
ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks -
Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh:
DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy"));
ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanksWouldn't this be easier ? DateTime dt = new DateTime(cpCreateDate.SelectedDate.Year, cpCreateDate.SelectedDate.Month, cpCreateDate.SelectedDate.Day) ? A lot less expensive than string to date conversions.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh:
DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy"));
ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks -
//you can use substring method like this:
cpCreateDate.SelectedDate.ToString("dd/MM/yyyy").Substring(0,10)
-
Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh:
DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy"));
ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanksI think you are trying to strip time because you think that small date time is only date field (lots of users think that, i don't know why, i guess there is something like that in Access), but that is not true. This data field just shortens range of valid values while keeping the time. Valid dates for a SMALLDATETIME column can range from January 1, 1900 to June 6, 2079. If I'm wrong, and that is not what error is about than you can simply strip time with: cpCreateDate.SelectedDate.Date;