5

I consume a web service which returns me some dates as string, and I use DateTime.Parse to get the correspondente DateTime objects. It is working, but I'm afraid my usage of DateTime.Parse may be vulnerable to bugs caused by different locale settings. The date returned is in the following format:

2014-04-24T00:00:00

The code I use to parse it:

DateTime d = DateTime.Parse(strValue);

Is there some way (such as passing a format provider, or using another method) in which I guarantee that my parsing routine will work regardless of the machine locale settings?

Doug
  • 6,322
  • 3
  • 29
  • 48
  • 2
    This is ISO8601 date time format that *does not* change depending on machine settings. Passing culture would not hurt, but optional. – Alexei Levenkov Apr 29 '13 at 19:39
  • Also be aware that the result of any of these will have a `.Kind` value of `Unspecified` - which may or may not be what you were expecting. – Matt Johnson-Pint Apr 29 '13 at 20:04

5 Answers5

5

If you want to parse a date independent of the user's locale, then use the invariant culture:

DateTime d = DateTime.Parse(strValue, CultureInfo.InvariantCulture);
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Yes, your answer will work, but it will work with any culture. – Martin Mulder Apr 29 '13 at 19:50
  • @MartinMulder It's not clear what you mean. This code will behave the same way in the context of any culture: it will parse numbers according to the invariant culture, and ignore the culture rules for the current context. This appears to be what the OP desires. – cdhowie Apr 29 '13 at 19:51
  • Yes, but this is more than he want. Check out my answer. It does not matter what culture he is using (en-US, nl-NL, Invaruant, etc.). The format he is using is ALWAYS parsed correctly, so you do not have to provider a culture explicitly. – Martin Mulder Apr 29 '13 at 19:53
5

You are using the shortest form! Your format is ISO 8601-format (http://nl.wikipedia.org/wiki/ISO_8601). It is recognized with any culture!

So your way is the simplest way: DateTime result = DateTime.Parse("2008-06-15T21:15:07");

If you are not sure, use: DateTime result = DateTime.ParseExact("2008-06-15T21:15:07", "s", null);

Check out: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx#properties

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
1

Since you have an exact format, I'd use a non-ambiguous format string:

DateTime.ParseExact("2014-04-24T00:00:00", "yyyy\\-MM\\-dd\\THH\\:mm\\:ss", null)
// or to reduce the C#-escaped backslashes:
DateTime.ParseExact("2014-04-24T00:00:00", @"yyyy\-MM\-dd\THH\:mm\:ss", null)

The escaped hyphens and colons, as well as the escaped T, mean that those are constant values. So this line should work regardless of any other factors.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • A big solution while "s" would have been much shorter! – Martin Mulder Apr 29 '13 at 19:45
  • 1
    If "s" works for you, go for it. However, I feel the clarity of spelling it out is helpful for anyone reading your code or troubleshooting in the future. It removes any ambiguity, and doesn't rely on any built-in standards - it just works, and will never not work, and you'll never need to look at the documentation to find out why it does or doesn't work. – Joe Enos Apr 29 '13 at 20:13
1

Yes, you could use the DateTime.ParseExact

string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

dateString = "2013-04-29T00:00:00";
format = "s";

result = DateTime.ParseExact(dateString, format, provider);

Where the "s" format string represents a sortable DateTime (MSDN on Format Strings)

Niclas
  • 1,220
  • 6
  • 12
0
  string dateString;
  DateTime dateValue;

  // Parse a string.
  dateString = "2014-04-24T00:00:00"; 
  if (DateTime.TryParseExact(dateString, "o",  CultureInfo.InvariantCulture, 
                DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
             dateValue.Kind);
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23