Reputation: 23
We have a WPF application that is used by users across the world. We have the below date conversation in the code.
DateTime dt = <some date time value>;
var formattedDateTime = (dt.ToString("yyyy.MM.dd-HH_mm_ss"));
This line is giving incorrect results in a few Japanese laptops with a Japanese culture set.
For example, if the dt value is {2019/09/11 14:42:47}:
'formattedDateTime' output in few Japanese laptops: 01.09.11-14_42_47
'formattedDateTime' output in all other cultures: 2019.09.11-14_42_47
I think some of the Japanese culture settings on these laptops are causing this issue. But I couldn't sort out what is that setting. Also, I think adding CultureInfo.InvariantCulture to the DateTime.ToString() may solve this issue.
Any idea what setting is causing this issue?
Upvotes: 2
Views: 956
Reputation: 38785
From what you've said, on certain machines the Japanese culture is defaulting to using the Japanese calendar rather than the Gregorian calendar.
The Japanese calendar uses eras based on the reign of the Emperor, such that:
We can test this in code:
var jpCulture = new CultureInfo("ja-jp");
jpCulture.DateTimeFormat.Calendar = new JapaneseCalendar();
// output: 昭和 63/05/11
Console.WriteLine(new DateTime(1988, 05, 11).ToString("g yyyy/MM/dd", jpCulture));
// output: 平成 20/05/11
Console.WriteLine(new DateTime(2008, 05, 11).ToString("g yyyy/MM/dd", jpCulture));
// output: 平成 31/04/30
Console.WriteLine(new DateTime(2019, 04, 30).ToString("g yyyy/MM/dd", jpCulture));
// output: 令和 01/05/01
Console.WriteLine(new DateTime(2019, 05, 01).ToString("g yyyy/MM/dd", jpCulture));
Your options here are:
string myDateString = DateTime.Now.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.DateTimeFormat.Calendar = new GregorianCalendar();
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
ToString
functionality and adds the era if the current calendar isn't Gregorian:public static class DateTimeExtensions
{
public static string ToStringYMDWithEra(this DateTime dt)
{
string formatString = (CultureInfo.CurrentCulture.Calendar is GregorianCalendar)
? "yyyy/MM/dd"
: "gyyyy/MM/dd";
return dt.ToString(formatString);
}
}
Usage: string formattedDate = DateTime.Now.ToStringYMDWithEra();
Note that Japanese users will implicitly understand the Japanese calendar with the era attached. Everything from forms to register with a doctor to anything official uses the Japanese calendar system. For example, my driving licence has my birth year (1988) as 昭和63.
Upvotes: 2
Reputation: 1374
Some of your users have their Windows date format set to use the Japansese calendar, in which 2019 is year 1 of the Reiwa era. They may be able to work around your issue by going into the Windows control panel and either changing their date format to one of the options that shows the Gregorian year ("ggy" instead of "y") or else change their calendar type from Japanese to Gregorian.
Upvotes: 3
Reputation: 327
const string culture = "ja-JP";
CultureInfo info = new CultureInfo(culture);
var formattedDateTime = (dt.ToString("yyyy.MM.dd-HH_mm_ss" , info));
Upvotes: -2