DateTimeFormat.AbbreviatedMonthNames adding a dot

2019-01-28 22:10发布

问题:

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

  1. ene.
  2. feb.
  3. mar.

on our new 2012 env while on 2008 ouptus:

  1. ene
  2. feb
  3. mar

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.

回答1:

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.