Keeping datetime culture invariant

  • Thread starter Thread starter helveticus
  • Start date Start date
H

helveticus

Is there a way to keep datetime expressions culture invariant? My app
is culture dependent and contains a series of datetime settings that
are saved in cookies. The cookie date strings raise exceptions when
they are read back since they won't be recognized in certain cultures
(ie US MM/DD/YY vs EU DD.MM.YY)? Currently, I track the culture state,
but this is cumbersome.

Is there a simple way to force the run-time to keep all dates in a
single format regardless of the culture state? I would like to avoid
erasing all cookies on culture change.

TIA for any hints. (ASP.NET 3.5)
 
helveticus said:
Is there a way to keep datetime expressions culture invariant? My app
is culture dependent and contains a series of datetime settings that
are saved in cookies. The cookie date strings raise exceptions when
they are read back since they won't be recognized in certain cultures
(ie US MM/DD/YY vs EU DD.MM.YY)? Currently, I track the culture state,
but this is cumbersome.

Is there a simple way to force the run-time to keep all dates in a
single format regardless of the culture state? I would like to avoid
erasing all cookies on culture change.

TIA for any hints. (ASP.NET 3.5)

I recommend keeping to the ISO 8601 standard for culture independent
dates. It's unambiguos, so there is minimal risk that it's misunderstood.

Use the string "yyyy-MM-dd" to format and parse dates:

string formatted = someDateTime.ToString("yyyy-MM-dd");

DateTime parsed = DateTime.ParseExact(someString, "yyyy-MM-dd",
CultureInfo.InvariantCulture);
 
Back
Top