System.Globalization 這個命名空間很有趣,竟然有 TaiwanCalendar 與 TaiwanLunisolarCalendar 類別!

TaiwanCalendar 是指台灣使用的曆法,也就是「民國年」的計算法。


[code:c#]
DateTime now = DateTime.Now;
TaiwanCalendar tc = new TaiwanCalendar();

int year = tc.GetYear(now);
MessageBox.Show(year.ToString());
// Output : 96

int month = tc.GetMonth(now);
// Output : 12

int daysOfMonth = tc.GetDaysInMonth(year, month);
MessageBox.Show(daysOfMonth.ToString());
// Output : 4
[/code]


TaiwanLunisolarCalendar 是指台灣使用的陰陽曆(農曆)。和 TaiwanCalendar 相同,但可以使用西元年份計算,而日期和月份則使用陰陽曆計算,真的很省事!


[code:c#]
TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

// 取得目前支援的農曆日曆到幾年幾月幾日( 2051-02-10 )
tlc.MaxSupportedDateTime.ToShortDateString();

// 取得今天的農曆年月日
txtContent.Text =
 tlc.GetYear(DateTime.Now).ToString() + "-" +
 tlc.GetMonth(DateTime.Now).ToString() + "-" +
 tlc.GetDayOfMonth(DateTime.Now).ToString();
[/code]

arrow
arrow
    全站熱搜

    akilina108 發表在 痞客邦 留言(0) 人氣()