よく忘れるので調べたついでに備忘録.
やること
System.Globalization.CultureInfo
にja-JP
を指定DateTimeFormat.Calendar
にJapaneseCalendar
を設定DateTime
に「1.」のCultureInfo
を設定- 文字列とする際に書式と
CultureInfo
を設定
varci1=newCultureInfo("ja-JP"){DateTimeFormat={Calendar=newJapaneseCalendar()}};Console.WriteLine(newDateTime(1926,12,25).ToString("ggyy年MM月dd日",ci1));Console.WriteLine(newDateTime(2018,9,1).ToString("ggyy年MM月dd日",ci1));Console.WriteLine(DateTime.Now.ToString("ggyy年MM月dd日",ci1));
こちらの方が良いと思う.
varci2=newCultureInfo("ja-JP");ci2.DateTimeFormat.Calendar=newJapaneseCalendar();Console.WriteLine(newDateTime(1926,12,25).ToString("ggyy年MM月dd日",ci2));Console.WriteLine(newDateTime(2018,9,1).ToString("ggyy年MM月dd日",ci2));Console.WriteLine(DateTime.Now.ToString("ggyy年MM月dd日",ci2));
出力結果
昭和元年12月25日
平成30年09月01日
令和02年08月14日
参考
なんやかんやでまだMSのリファレンスに慣れていない自分がいる...
https://docs.microsoft.com/ja-jp/dotnet/api/system.globalization.cultureinfo?view=netcore-3.1