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/2011en-IA (English India): 24-10-2011en-ZA (in English in South Africa): 2011/10/24en-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"
Ian boyd
source share