DateTime problem when global server culture is different on different servers

My site is hosted on several servers in different places

The data format culture is different everywhere - we use the mm/dd/yyyy format every time, but if the dd/mm/yyyy culture is installed on some server, our site throws a Datetime exception.

+7
source share
4 answers

You must specify which culture you want to use when you convert a string to a date.

The culture you must use depends on which culture the dates are formatted. For example, if all finalized dates are formatted as Slovak :

 String s = "24. 10. 2011"; 

Then you need to analyze the line as if it were in the culture of Slovak (Slovakia) ( sk-SK ):

 //Bad: d = DateTime.Parse(s); //Good: d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("sk-SK")); //Slovak (Slovakia) 

If your dates are in Tajik (Kyrgyz Tajikistan) , you need to analyze it as tg-Cryl-Tj :

 String s = "24.10.11" DateTime d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("tg-Cryl-Tj")); 

Which leads to the question: what date format are you using? You do not have to rely on the server locale setting, you have to decide which format you want.

 //Bad String s = d.ToString(); //Good String s = d.ToString(CultureInfo.CreateSpecificCulture("si-LK")); //Sinhala (Sri Lanka) //s = "2011-10-24 12:00:00 පෙ.ව." 

I suspect that you prefer to do everything in English. But then you need to decide which version of English:

  • en-AU (English Austrailia): 24/10/2011
  • en-IA (English India): 24-10-2011
  • en-ZA (in English in South Africa): 2011/10/24
  • en-US (English): 10/24/2011

I suspect I prefer English (India) ( en-IA ).


But if you really can’t decide which culture to use when converting dates to strings and vice versa, and dates should never be displayed to the user, then you can use Invariant culture :

 String s = "10/24/2011" //invariant culture formatted date d = DateTime.Parse(s, CultureInfo.InvariantCulture); //parse invariant culture date s = d.ToString(CultureInfo.InvariantCulture); //convert to invariant culture string 
+11
source

Never, ever, store dates within yourself as strings. Not in the database, not in your application.

If you need to move date values ​​between servers, go to the binary. Or, if you really need to use strings, use ToString(CultureInfo.InvariantCulture) - or just serialize the Ticks property.

Also, never pass dates as strings to the database using SQL commands that you create using code. Use SqlParameter for this or even better, rely on some O / R Mapper such as Entity Framework or Linq to SQL.

+1
source

Never rely on the default server locale. For your case, this means:

  • Use prepared statements in which you pass a date as a (unformatted) date object, rather than as a (formatted) string object. You should never use strings to represent dates in your application in any case, since you cannot perform functions defined by date on them (for example, adding 1 month, getting the last day of the current week, etc.).

  • Use SQL functions like to_date and to_char everywhere (the exact names depend on your DBMS) if you really need to use string objects in your application

0
source

When deploying to a server that is not under your control, it is vital to ensure that your code does not have hard-coded culture dependencies.

Most likely, you will want to find your code for DateTime.Parse or similar. We have a set of extension methods on DateTime that we use instead for the right culture.

0
source

All Articles