答案, 還是Yes!!
.NET Framework (2.0以上)的類別庫中有一個TaiwanLunisolarCalendar 類別(參考: http://msdn2.microsoft.com/zh-tw/library/system.globalization.taiwanlunisolarcalendar.aspx)
這個類別的可看性就多囉!
它與一般的西曆及國曆是很不一樣的, 尤其可能有閏月. 要顯示完整的年月日, 至少要用到以下幾個方法:
- GetSexagenaryYear
- GetCelestialStem
- GetTerrestrialBranch
- GetMonth
- GetLeapMonth
- GetDayOfMonth
下列程式是完整的範例:
Dim cal As New TaiwanLunisolarCalendar()
Dim now As Date = Date.Now.AddYears(-1)
Dim 天干 As String = "甲乙丙丁戊己庚辛壬癸"
Dim 地支 As String = "子丑寅卯辰巳午未申酉戌亥"
Dim lun60Year As Integer = cal.GetSexagenaryYear(now)
Dim 天干Year As Integer = cal.GetCelestialStem(lun60Year) - 1
Dim 地支Year As Integer = cal.GetTerrestrialBranch(lun60Year) - 1
Dim lunMonth As Integer = cal.GetMonth(now)
Dim leapMonth As Integer = cal.GetLeapMonth(cal.GetYear(now))
If leapMonth > 0 AndAlso lunMonth >= leapMonth Then
lunMonth -= 1
End If
Dim lunDay As Integer = cal.GetDayOfMonth(now)
MessageBox.Show(String.Format("農曆:{0}年{1}月{2}日", _
天干(天干Year) & 地支(地支Year), lunMonth, lunDay))
TaiwanLunisolarCalendar cal =new TaiwanLunisolarCalendar() ;
DateTime now = DateTime.Now.AddYears(-1) ;
String 天干= "0甲乙丙丁戊己庚辛壬癸" ;
String 地支= "0子丑寅卯辰巳午未申酉戌亥" ;
int lun60Year = cal.GetSexagenaryYear(now) ;
int 天干Year = cal.GetCelestialStem(lun60Year) ;
int 地支Year = cal.GetTerrestrialBranch(lun60Year) ;
int lunMonth = cal.GetMonth(now) ;
int leapMonth = cal.GetLeapMonth(cal.GetYear(now)) ;
if (leapMonth > 0 && lunMonth >= leapMonth )
{
lunMonth -= 1 ;
}
int lunDay = cal.GetDayOfMonth(now) ;
MessageBox.Show(String.Format("農曆:{0}年{1}月{2}日",
天干[天干Year].ToString() + 地支[地支Year].ToString(), lunMonth, lunDay));
以今天的日期(AddYears(-1))來執行, 得到的結果會是(去年的今天):
農曆:丁亥年2月2日
沒想到 .Net 有提供陰曆的 class,太妙了,看來可以加個農曆生日提醒的功能在系統裡~ :-P
回覆刪除您好:請問農曆:{0}年{1}月{2}日 是從哪裡來的?謝謝
回覆刪除{0} 這是string.Format方法的格式表現方式
回覆刪除代表這裡要填上後面第二({0})三({1})..個參數的值.