简体   繁体   中英

DateTime.TryParseExact fails to parse a particular date on Windows 7 but works on Windows 10

When I run the code below, on Windows 10 it successfully parses the date and outputs "2012-03-14" whereas on Windows 7 it fails to parse the date. I tested with both .NET 4.0 and 4.6.1 and the culture "gsw-FR" exists on both computers. Why does this not work correctly when run on Windows 7?

if (DateTime.TryParseExact("Mettwuch 14 März 2012", "dddd d MMMM yyyy", new CultureInfo("gsw-FR"), DateTimeStyles.None, out var dt))
    Console.WriteLine(dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
else
    Console.WriteLine("Parsing Error");

Upon outputting the formatted date pattern for the date 2012-03-14 on both Windows 7 and Windows 10, I noticed that the day of the week (corresponding to format code "dddd") was spelled differently

Console.WriteLine(new DateTime(2012, 3, 14).ToString("dddd d MMMM yyyy", new CultureInfo("gsw-FR")));

Windows 7 outputs: Mittwuch 14 März 2012
Windows 10 outputs: Mettwuch 14 März 2012

Likewise if you format a cell in Microsoft Excel with the date value 2012-03-14 using the NumberFormat "[$-10484]dddd d mmmm yyyy;@" , it formats the date as follows:

Excel 2007/2010 on Windows 7: Mittwuch 14 März 2012
Excel 2013/2016/365 on Windows 7: Mettwuch 14 März 2012
Excel 2007/2010/2013/2016/365 on Windows 10: Mettwuch 14 März 2012

.NET does not use its own localization data on Windows, the localization data is part of the operating system which is why the same date format code differs in this example. In this case, Windows 7 and Windows 10 have different localization data for spelling that particular day of the week using the gsw-FR locale. Additionally, it appears that Excel has peculiar behavior in that different versions (2010 vs 2013) of Excel on Windows 7 output different formatted values and additionally the same Excel version (2007 or 2010) on different operating systems (Windows 7 and Windows 10) output different formatted values. So it is not clear if Excel is using its own localization data or the operating system's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM