asp.net - UI culture and dates

Don't rely on DateTime.Now.ToString() to populate varchar database fields that are used for holding dates.  Or even better still don't use varchar fields for holding dates in the database - unless you've inherited this scenario and don't want the risk/time to factor it out.

If, for example, the user inadvertantly has their browser language/culture set to say en-us instead of en-gb you'll get a date like so '03/31/2013 09:00:00 AM' instead of '31/03/2013 09:00:00'.  This will then make any date processing you do on this field go SPLAT.  Other developers/project manager may be mildly curious as to why you're using a varchar field to hold a date, at which time you will promptly blame someone else for it :)  (He did, Ed.)

Solution, set the culture in the web.config

<globalization culture="en-gb" uiCulture="en-gb"  />

There is no quick and dirty, only dirty.

 

Comments

Find out more