找到民國(TaiwanCalendar)與農曆(TaiwanLunisolarCalendar)的日曆類別

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]

  

此文章由 will 發表於 2007/12/4 下午 12:09:00

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: .Net | C#

標籤: , , ,

收藏:

相關文章

新增評論


(將顯示您的Gravatar圖示)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



線上預覽

八月 29. 2008 11:08