Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a form which saves data into sql db. the date is saved in the database like
2012-10-25 00:00:00:000. how can i change the date format so it saves into the
database like 25-10-2012 only (dd.mm.yyyy)
thanks
Posted

Just to add to what Marcus says, conversion from a string to DateTime should be done as early as physically possible, and conversion from a DateTime to a string should be done as late as possible.

The reasons for this are to do with localisation - the earlier you convert from a string, the more likely you are to be able to access the locale information that the user has set, and the more likely you are that the conversion will be correct and the end result what the user intended to enter.
For example, if the user enters 10/11/12 does he mean 10th November 2012, 11th Oct 2012, or 12th November 2010? If you have access to the locale information you can tell and parsing of the string can be done correctly.

The later you convert back to a string, the more likely you are to generate an understandable date fro teh user - who may have a totally different locale and date format.

Remember that DateTime does not store a date or time as such - it stores it as an interval in milliseconds since a fixed point in time. So conversion to or from a string with reference to the locale the user is expecting means that date can be correct even if the dating system is not Gregorian - Hirji for example uses the lunar year in which Muhammad immigrated from Makkah to Madinah.
The Gregorian date for today 25 Oct 2012 is (approximately) 9 Thw al-Hijjah 1433 in Hirji.
Converting as I describe means this doesn't have to complicate your software at all.
 
Share this answer
 
The simple answer is, you don't.
If you want at any time to do date calculations within the database keep the fields defined as such.
When you pull your data back into C#, just format the date as you wish it to appear.
DateTime Formatting C#[^]
 
Share this answer
 
v2
Comments
RaisKazi 25-Oct-12 9:45am    
Agree. 5ed!
mikeoabban 25-Oct-12 9:57am    
how do i format it to appaer like that on the form
fjdiewornncalwe 25-Oct-12 10:04am    
See the link in my updated solution.

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