Keeping datetime culture invariant

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)
 
G

Göran Andersson

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top