2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#で日付を和暦で表示したい場合

Last updated at Posted at 2021-07-28

C#のコードを書く際に、日付を和暦で表示したい場合の方法についてまとめた。

日付を和暦で表示するための準備

まずは名前空間に

using System.Globalization;

を定義する。

そして、クラスやメソッド内にCultureInfoクラスを定義する。引数は「ja-JP」。

var culture = new CultureInfo("ja-JP"); 
culture.DateTimeFormat.Calendar = new JapaneseCalendar();

こうすることで、日付を和暦で表示する準備が整った。

日付を和暦で表示する。

var date1 = new DateTime(2001, 12, 25);
Console.WriteLine(date1.ToString("ggyy年M月d日", culture)); // 平成13年12月25日

元号は「gg」で取得できる。

曜日を日本語で取得する

var dayOfWeekJapanese = culture.DateTimeFormat.GetDayName(date1.DayOfWeek);
Console.WriteLine(dayOfWeekJapanese); // 火曜日

元号名を取得する

また、ただ元号名を取得したい場合は、

var gengo = culture.DateTimeFormat.Calendar.GetEra(date1);
var gengoName = culture.DateTimeFormat.GetEraName(gengo);
Console.WriteLine(gengoName);// 平成

参考文献

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?