準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
using System.Globalization;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
enum Gengo
{
明治 = 0,
大正 = 1,
昭和 = 2,
平成 = 3,
令和 = 4,
}
private void button1_Click(object sender, EventArgs e)
{
var jc = new JapaneseCalendar();
var dt = new DateTime(2023, 8, 15);
var ThisEra = (Gengo)(jc.GetEra(dt) - 1);
string s1 = string.Format("{0} {1} 年 {2} 月 {3} 日", ThisEra.ToString()
, jc.GetYear(dt)
, jc.GetMonth(dt)
, jc.GetDayOfMonth(dt));
listBox1.Items.Add(s1);
}
}
}
解説
今日の日付を和暦で出力しています。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.04.26 Visual Studio 2015 Professional (C# 6.0)
