原來還準備自己寫演算法,並研究農曆規則。發現那太難和麻煩了,光是農曆的推算那就我等專門研究曆法的人一下搞懂的。後來發現。NET類庫也提供一些基礎的農曆類System.Globalization.ChineseLunisolarCalendar。我改裝了一下如DateTime時間形式。代碼如下。實現了 西曆農曆轉換的功能。但是只能算到1900~2100年之間的。基本夠日常使用了。原始碼如下。
using System;
using System.Collections.Generic;
using System.Text;
namespace System
...{
/**//// <summary>
/// 中國常用農曆日期時間類
/// </summary>
class ChinaDateTime
...{
private int year, month, dayOfMonth;
private bool isLeap;
public DateTime time;
/**//// <summary>
/// 擷取當前日期的農曆年
/// </summary>
public int Year
...{
get ...{ return year; }
}
/**//// <summary>
/// 擷取當前日期的農曆月份
/// </summary>
public int Month
...{
get ...{ return month; }
}
/**//// <summary>
/// 擷取當前日期的農曆月中天數
/// </summary>
public int DayOfMonth
...{
get ...{ return dayOfMonth; }
}
/**//// <summary>
/// 擷取當前日期是否為閏月中的日期
/// </summary>
public bool IsLeap
...{
get ...{ return isLeap; }
}
System.Globalization.ChineseLunisolarCalendar cc;
/**//// <summary>
/// 返回指定西曆日期的陰曆時間
/// </summary>
/// <param name="time"></param>
public ChinaDateTime(DateTime time)
...{
cc = new System.Globalization.ChineseLunisolarCalendar();
if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
throw new Exception("參數日期時間不在支援的範圍內,支援範圍:" + cc.MinSupportedDateTime.ToShortDateString()+"到"+cc.MaxSupportedDateTime.ToShortDateString());
year = cc.GetYear(time);
month = cc.GetMonth(time);
dayOfMonth = cc.GetDayOfMonth(time);
isLeap = cc.IsLeapMonth(year, month);
if (isLeap) month -= 1;
this.time = time;
}
/**//// <summary>
/// 返回當前日前的農曆日期。
/// </summary>
public static ChinaDateTime Now
...{
get ...{ return new ChinaDateTime(DateTime.Now); }
}
/**//// <summary>
/// 返回指定農曆年,月,日,是否為閏月的農曆日期時間
/// </summary>
/// <param name="Year"></param>
/// <param name="Month"></param>
/// <param name="DayOfMonth"></param>
/// <param name="IsLeap"></param>
public ChinaDateTime(int Year, int Month, int DayOfMonth, bool IsLeap)
...{
if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year)
throw new Exception("參數年份時間不在支援的範圍內,支援範圍:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());
if (Month < 1 || Month > 12)
throw new Exception("月份必須在1~12範圍");
cc = new System.Globalization.ChineseLunisolarCalendar();
if(cc.GetLeapMonth(Year)!=Month&&IsLeap)
throw new Exception("指定的月份不是當年的閏月");
if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
throw new Exception("指定的月中的天數不在當前月天數有效範圍");
year = Year;
month = Month;
dayOfMonth = DayOfMonth;
isLeap = IsLeap;
time = DateTime.Now;
}
/**//// <summary>
/// 擷取當前農曆日期的西曆時間
/// </summary>
public DateTime ToDateTime()
...{
return cc.ToDateTime(year, isLeap ? month + 1 : month, dayOfMonth, time.Hour, time.Minute, time.Second, time.Millisecond);
}
/**//// <summary>
/// 擷取指定農曆時間對應的西曆時間
/// </summary>
/// <param name="CnTime"></param>
/// <returns></returns>
public static DateTime ToDateTime(ChinaDateTime CnTime)
...{
return CnTime.ToDateTime();
}
/**//// <summary>
/// 擷取指定西曆時間轉換為農曆時間
/// </summary>
/// <param name="Time"></param>
/// <returns></returns>
public static ChinaDateTime ToChinaDateTime(DateTime Time)
...{
return new ChinaDateTime(Time);
}
/**//// <summary>