Japanese calendar display in C #

less than 1 minute read

I often forget it, so I checked it and made a memorandum.

things to do

  1. Specify ja-JP in System.Globalization.CultureInfo
  2. Set JapaneseCalendar in DateTimeFormat.Calendar
  3. Set CultureInfo of” 1. “in DateTime
  4. Set the format and CultureInfo when 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

Tags:

Updated: