Phanix

Just writing

Datetime format - 顯示非西元的紀年方式

例如: 平成XX年

DateTime dt = DateTime.Parse("2014/09/26");
CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.Calendar = new JapaneseCalendar();
Thread.CurrentThread.CurrentCulture = culture;
ViewData["date"] = dt.ToString("F", culture);

上面這個寫法會把整個 thread 的 culture info 改成 ja-JP,不是那麼好用。

下面的寫法比較好,利用CultureInfor做為formatter,在呼叫 .ToString() 時作用。

DateTime dt = DateTime.Parse("2014/09/26");
CultureInfo ci = new CultureInfo("ja-JP");
Calendar cal = new JapaneseCalendar();
ci.DateTimeFormat.Calendar = cal;
string strWareki = dt.ToString("ggyy/MM/dd", ci);
Console.WriteLine(strWareki);

Ref:
Culture Info
Language Culture Code

Like my work??
Don't forget to support or like, so I know you are with me..

CC BY-NC-ND 2.0

Want to read more ?

Login with one click and join the most diverse creator community.