Reputation: 81
Last night we migrated our web services tier from physical Windows 2008 r2 to virtual Windows 2012.
We are getting tons of events on ours logs about DateTime's invalid formats, strange as we double checked our regional settings.
Long story short:
CultureInfo.GetCultureInfo("es-MX").DateTimeFormat.AbbreviatedMonthNames
Outputs (using LinqPad5):
on our new 2012 env while on 2008 ouptus:
Our parsing is something like this:
DateTime.Parse("18 ene 16",CultureInfo.GetCultureInfo("es-MX"))
And while it worked wonders now it throws
FormatException : The string was not recognized as a valid DateTime. There is an unknown word starting at index 3.
While
DateTime.Parse("18 ene. 16",CultureInfo.GetCultureInfo("es-MX"))
works but isn't the expected input from our several clients.
Working on the same runtime version (4.0.30319.42000), double checked (again) our regional settings on both servers what else can I look for to fix this (before giving up and hacking it with a regex replace)?
Thanks.
Upvotes: 3
Views: 1464
Reputation: 1
I ran into the same problem, when scraping a web-element for a date. The date looked like this "Fre 14. okt 2022".
Inspired by DSXP answer, I found this to be working:
var cultureInfo = new CultureInfo("da-DK");
cultureInfo.DateTimeFormat.AbbreviatedMonthNames = cultureInfo.DateTimeFormat.AbbreviatedMonthNames
.Select(x => x.EndsWith(".") ? x.TrimEnd('.') : x)
.ToArray();
Upvotes: 0
Reputation: 81
Sadly I couldn't find anything in the configuration that pointed me in the right direction.
Ended with a nasty hack like this:
var cultura = CultureInfo.CreateSpecificCulture("es-MX");
if (cultura.DateTimeFormat.AbbreviatedMonthNames.First().EndsWith("."))
cultura.DateTimeFormat.AbbreviatedMonthNames = cultura.DateTimeFormat.AbbreviatedMonthNames.Select(c => c.Substring(0, c.Length > 1 ? c.Length - 1 : 0)).ToArray();
and used that culture in the parsing.
Upvotes: 3