Reputation: 28312
When attempting to parse a date with the culture es-ES
I am getting different behavior locally than when running the same code on the build server and when using dotnetfiddle.
My test program that highlights this problem is as follows:
public static void Main(string[] args)
{
var when = new DateTime(2018, 01, 19);
CultureInfo dateCulture = new CultureInfo("es-ES");
var headerDate = "19-ene-2018";
bool success;
DateTime result;
Console.WriteLine(when.ToString("dd-MMM-yyyy", dateCulture));
success = DateTime.TryParseExact(
headerDate,
"dd-MMM-yyyy",
dateCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
out result);
Console.WriteLine(success);
Console.WriteLine(result);
}
Locally this produces:
19-ene-2018
True
1/19/2018 12:00:00 AM
Whereas on on dotnetfiddle it produces:
19-ene.-2018
False
1/1/0001 12:00:00 AM
The first surprising thing is that using the exact same date format the server formats the month as ene.
where as locally it is ene
(notice the extra period). This is what I believe is causing the actual date not to parse.
Before giving up and either using a regex or some other equally horrible solution to this does anyone have any ideas as to why this could be happening? Is this a difference between the server and desktop framework installations?
Upvotes: 1
Views: 929
Reputation: 4077
Not sure why that extra period is produced in dotnetfiddle specifically. But, using CultureInfo
with the traditional sort order works perfectly on local as well as on dotnetfiddle (also mentioned by Dirk; es-ES_tradnl
):
CultureInfo dateCulture = new CultureInfo(0x040A);
Upvotes: 0
Reputation: 14477
While I can't explain why "es-ES" culture can have different AbbreviatedMonthNames
. Here is an solution to it:
Depending whether the period is expected or not, you can modify AbbreviatedMonthNames
with this:
CultureInfo dateCulture = new CultureInfo("es-ES");
dateCulture.DateTimeFormat.AbbreviatedMonthNames = dateCulture.DateTimeFormat.AbbreviatedMonthNames
// uncomment one of the two lines below
//.Select(x => x.TrimEnd('.'))
//.Select(x => !string.IsNullOrEmpty(x) && x.EndsWith(".") ? x : x + ".")
.ToArray();
Upvotes: 2