1

Example : "02/03/2014" mm/dd/yyyy (US) and "03/02/2014" dd/mm/yyyy (French).

They are the same date but different region of the world.

I know I need to convert them to date objects then compare them.

Is there a way to dynamically pull and set culture info from the system, so that the the date format will be specific to the region with out hard coding the culture info?

Hard coding -Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

user3267822
  • 11
  • 1
  • 3
  • "But what is the simplest and most efficient way to to get them in the same format " - by searching SO before asking perhaps? – Mitch Wheat Feb 04 '14 at 00:09
  • `DateTime.Parse` using the user's native locale. – p.s.w.g Feb 04 '14 at 00:09
  • Unfortunately, "locale" is an interesting subject. The system locale might not be what the users expected format/locale is, especially when the C# code runs remotely (such as ASP.NET). I recommend being explicit whenever possible. – user2864740 Feb 04 '14 at 00:14
  • There is an overload to the `DateTime.Parse` that lets you specify the culture. – Magnus Feb 04 '14 at 00:23
  • @Magnus Yes :) Through-and-through it needs to be handled with some defined rules (and indicated or well-understood in the UI). – user2864740 Feb 04 '14 at 00:24

4 Answers4

2

DateTime parsing is...hard.

You need to know the culture of the user who entered the date (or at least, the date format they used, which is presumably set by your User interface). Without that, you can't reliable convert the text date to a date time object as something like 2/3/2014. It could be either

  • Feb 3, 2014, if the format was month/day/year, as is the norm in the United States, or
  • March 2, 2014, if the format was day/month/year, as is the norm in most of the rest of the world.

If you know the user interface culture or the format used, you can use the appropriate overload of DateTime's Parse(), TryParse(), ParseExact() or TryParseExact().

Or you can roll you own date conversion method due to CultureInfo making certain...assumptions...regarding separators.

You might want to look at Jon Skeet's Noda Time (http://nodatime.org/), but I don't know if it supports parsing.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

If you have a string that represents the date, e.g. "12/25/2014" and you want to use Date.Parse on it, because your date is in American format the Culture in your thread must be set to "en-US" http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture%28v=vs.110%29.aspx

Probably, if you try to do Date.Parse on "31/01/2014" it will fail. But if you will set thread.CurrentCulture to "fr-Fr" it will work.

"But what is the simplest and most efficient way to to get them in the same format...?".

DateTime is DateTime. It is not stored in specific format. It is your culture that determines format.

From personal experience - we have international application and each user could literally pick the language in which application displayed its screens and how it displayed dates. At the startup we would determine the chosen language/locality and we would set it on the thread. Voila - people now can enter date in their local format and program would parse it correctly.

T.S.
  • 18,195
  • 11
  • 58
  • 78
1

I have found that using the format and the culture to be the most reliable method.

    var date = DateTime.Now;
    var format = new CultureInfo("en-GB");
    var stringDate = date.ToString("d",format);

this way you know exactly what will be returned independently of the culture of the system.

Martin T
  • 11
  • 1
0

As I understand it the underlying issue is different timezones. I'd recommend converting both into UTC and do your comparisons on that. SEE: http://msdn.microsoft.com/en-us/library/bb397769%28v=vs.110%29.aspx (.NET Framework 4.5 -- but you'll get the general idea).