ヘッダー
C# サンプル集
VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

日付を文字列に変換する例

2020/7/19

→ Visual Basic のサンプルに切り替える

 

"2004/08/05" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yyyy\/MM\/dd");

System.Diagnostics.Debug.WriteLine(st); //2004/08/05 と表示されます。

Debug.WriteLineが表示される場所

 

"2004/8/5" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yyyy\/M\/d");

System.Diagnostics.Debug.WriteLine(st); //2004/8/5 と表示されます。

Debug.WriteLineが表示される場所

 

"2004年08月05日" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yyyy年MM月dd日");

System.Diagnostics.Debug.WriteLine(st); //2004年08月05日 と表示されます。

Debug.WriteLineが表示される場所

 

"04/08/05" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yy\/MM\/dd");

System.Diagnostics.Debug.WriteLine(st); //04/08/05 と表示されます。

Debug.WriteLineが表示される場所

 

"20040805" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yyyyMMdd");

System.Diagnostics.Debug.WriteLine(st); //20040805 と表示されます。

Debug.WriteLineが表示される場所

 

 

"H16/08/05" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);

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

string st = sourceDate.ToString(@"ggy\/MM\/dd", Japanese);

switch(Japanese.DateTimeFormat.Calendar.GetEra(sourceDate))
{
    case 1: //明治
        st = st.Replace("明治", "M");
        break;
    case 2: //大正
        st = st.Replace("大正", "T");
        break;
    case 3: //昭和
        st = st.Replace("昭和", "S");
        break;
    case 4: //平成
        st = st.Replace("平成", "H");
        break;
    case 5: //令和
        st = st.Replace("令和", "R");
        break;
}

System.Diagnostics.Debug.WriteLine(st); //H16/08/05 と表示されます。

 → Debug.WriteLineが表示される場所

参考:和暦と西暦を変換する

 

別のやり方

DateTime sourceDate = new DateTime(2004, 8, 5);

var japaneseCalendar = new JapaneseCalendar();
string eraPredix = new string[] { "M", "T", "S", "H", "R" }[japaneseCalendar.GetEra(sourceDate) - 1];
string st = string.Format(@"{0}{1}{2:\/MM\/dd}", eraPredix, japaneseCalendar.GetYear(sourceDate), sourceDate);

System.Diagnostics.Debug.WriteLine(st); // H16/08/05 と表示されます。

Debug.WriteLineが表示される場所

 

 

"平16年8月5日" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);

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

string st = sourceDate.ToString(@"ggy年M月d日", Japanese);

switch(Japanese.DateTimeFormat.Calendar.GetEra(sourceDate))
{
    case 1: //明治
        st = st.Replace("明治", "明");
        break;
    case 2: //大正
        st = st.Replace("大正", "大");
        break;
    case 3: //昭和
        st = st.Replace("昭和", "昭");
        break;
    case 4: //平成
        st = st.Replace("平成", "平");
        break;
    case 5: //令和
        st = st.Replace("令和", "令");
        break;
}

//デフォルトでは1年は「元年」です。「元年」ではなく「1年」にしたい場合、このコメントを解除します。
//st = st.Replace("元年", "1年");

System.Diagnostics.Debug.WriteLine(st); //平16年8月5日 と表示されます。

 → Debug.WriteLineが表示される場所

参考:和暦と西暦を変換する

 

別のやり方

DateTime sourceDate = new DateTime(2004, 8, 5);

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

string eraName = Japanese.DateTimeFormat.GetAbbreviatedEraName(japaneseCalendar.GetEra(sourceDate));

string st = string.Format("{0}{1}", eraName, sourceDate.ToString(@"y年M月d日", Japanese));

System.Diagnostics.Debug.WriteLine(st); //平16年8月5日 と表示されます。

Debug.WriteLineが表示される場所

メモ:1年は「元年」に変換されます。

 

"平成16年8月5日" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);

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

string st = sourceDate.ToString("ggy年M月d日", Japanese);

System.Diagnostics.Debug.WriteLine(st); //平成16年8月5日 と表示されます。

 → Debug.WriteLineが表示される場所

参考:和暦と西暦を変換する

メモ:1年は「元年」に変換されます。

 

"08.05.2004" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"MM\.dd\.yyyy");

System.Diagnostics.Debug.WriteLine(st); //08.05.2004 と表示されます。

Debug.WriteLineが表示される場所

 

"200408" のように変換する例

DateTime sourceDate = new DateTime(2004, 8, 5);
string st = sourceDate.ToString(@"yyyyMM");

System.Diagnostics.Debug.WriteLine(st); //200408 と表示されます。

Debug.WriteLineが表示される場所