I am trying to parse a Dutch date from some logfiles but C# DateTime.TryParseExact is always returning false:
DateTime.TryParseExact("mei 21, 2015 12:25:35:719 AM", "MMM dd, yyyy hh:mm:ss:fff tt", new CultureInfo("nl-BE"), DateTimeStyles.None, out date)
Returns false; however I don't see what could be wrong with my date format?
However this returns true:
DateTime.TryParseExact("May 21, 2015 12:25:35:719 AM", "MMM dd, yyyy hh:mm:ss:fff tt", new CultureInfo("en-US"), DateTimeStyles.None, out date) true bool
So that would mean "nl-BE" does not know the word "mei", while en-US has no problem with "May". What can I do to overcome this?
It looks like that culture doesn't use an AM designator:
var culture = new CultureInfo("nl-BE");
Console.WriteLine("x{0}x", culture.DateTimeFormat.AMDesignator);
That prints xx
, suggesting that the AM designator is empty.
You can modify this though:
var culture = (CultureInfo) new CultureInfo("nl-BE");
culture.DateTimeFormat.AMDesignator = "AM";
culture.DateTimeFormat.PMDesignator = "PM";
DateTime date;
var result = DateTime.TryParseExact("mei 21, 2015 12:25:35:719 AM",
"MMM dd, yyyy hh:mm:ss:fff tt",
culture,
DateTimeStyles.None, out date);
...
See more on this question at Stackoverflow