Japanese calendar display in C #
I often forget it, so I checked it and made a memorandum.
things to do
- Specify
ja-JPinSystem.Globalization.CultureInfo - Set
JapaneseCalendarinDateTimeFormat.Calendar - Set
CultureInfoof” 1. “inDateTime - Set the format and
CultureInfowhen making a string
var ci1 = new CultureInfo("ja-JP")
{
DateTimeFormat = { Calendar = new JapaneseCalendar() }
};
Console.WriteLine(new DateTime(1926, 12, 25).ToString("ggyy year MM month dd day", ci1));
Console.WriteLine(new DateTime(2018, 9, 1).ToString("ggyy year MM month dd day", ci1));
Console.WriteLine(DateTime.Now.ToString("ggyy year MM month dd day", ci1));
I think this is better.
var ci2 = new CultureInfo("ja-JP");
ci2.DateTimeFormat.Calendar = new JapaneseCalendar();
Console.WriteLine(new DateTime(1926, 12, 25).ToString("ggyy year MM month dd day", ci2));
Console.WriteLine(new DateTime(2018, 9, 1).ToString("ggyy year MM month dd day", ci2));
Console.WriteLine(DateTime.Now.ToString("ggyy year MM month dd day", ci2));
Output result
December 25, 1945
September 01, 2018
Reiwa August 14, 2002
reference
Somehow, I’m not used to MS reference yet. .. ..
https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.cultureinfo?view=netcore-3.1