My website is hosted on multiple servers at different locations
Everywhere the Culture of the data format is different- we use mm/dd/yyyy
format every where but incase some server has the culture set to dd/mm/yyyy
then our website generates Datetime exception.
You should be specifying what culture you want to use whenever you convert a string to a date.
The culture you should be using depends on what culture the dates are formatted as. For example, if all dates you are parsing are formatted as Slovak:
String s = "24. 10. 2011";
Then you need to parse the string as though it were in Slovak (Slovakia) (sk-SK
) culture:
//Bad:
d = DateTime.Parse(s);
//Good:
d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("sk-SK")); //Slovak (Slovakia)
If your dates are all in Tajik (Tajikistan Cyrillic), then you need to parse 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 should not be relying on the locale setting of the server, you should be deciding what 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 have to decide which variant of English:
en-AU
(English Austrailia): 24/10/2011
en-IA
(English India): 24-10-2011
en-ZA
(English South Africa): 2011/10/24
en-US
(English United States): 10/24/2011
i suspect you prefer English (India) (en-IA
).
But if you really can't decide what culture to use when converting dates to strings and vice-versa, and the dates are never meant to be shown to a user, then you can use the 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
Never, ever, store dates internally as strings. Not in the database, not in your app.
If you need to move date values between servers, go binary. Or if you really really have to use strings, use ToString(CultureInfo.InvariantCulture)
- or simply serialize the Ticks
property.
Also, never pass dates as strings to the database using SQL commands that you build using code. Use SqlParameter
for that, or even better, rely on some O/R Mapper, such as Entity Framework or Linq to SQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With