很久之前的东西,又要用了弄出来整理一下
忘性比较大
阳历转农历
//该命名空间下
using System.Globalization;
//有专门计算
中国农历的类
ChineseLunisolarCalendar cnsc = new ChineseLunisolarCalendar();
ChineseLunisolarCalendar cnsc = new ChineseLunisolarCalendar();
DateTime jrLDate = new DateTime(2024, 1, 1);
int year = cnsc.GetYear(jrLDate);
// 是否有闰月,返回正整数(比如2023年闰2月,返回值为3)
int flag = cnsc.GetLeapMonth(year);
//有闰月则实际月份减1
int month = flag > 0 ? cnsc.GetMonth(jrLDate) - 1 : cnsc.GetMonth(jrLDate);
int day = cnsc.GetDayOfMonth(jrLDate);
MessageBox.Show("农历:"+year+"年"+month+"月"+day+"日");
农历转阳历
//接上序 这是把上面被转换成农历的日期 又转换回去了 所以输出的还是2024-01-01
month = flag > 0 ? month + 1 : month; //有闰月则月份加1后转换
DateTime ylDate = cnsc.ToDateTime(year, month, day, 0, 0, 0, 0);
MessageBox.Show("阳历:"+ylDate);
//可以直接调用输出农历对应阳历
DateTime ylDate = cnsc.ToDateTime(jrLDate.Year, jrLDate.Month, jrLDate.Day, 0, 0, 0, 0);
扩展
//调用类中的方法
DateTime jrLDate = DateTime.Now();
DateTime dtnl=ChinaDate.GetDateFromLunarDate(jrLDate);//农历转阳历
DateTime dtnl=ChinaDate.GetSunYearDate(jrLDate);//阳历转农历
//其他方法自行查看
/// <summary>
/// 农历与阴历之间相互转化工具类
/// </summary>
public class ChinaDate
{
#region 农历信息获取
private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();
private static Hashtable gHoliday = new Hashtable();
private static Hashtable nHoliday = new Hashtable();
private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
static ChinaDate()
{
//公历节日
gHoliday.Add("0101", "元旦");
gHoliday.Add("0214", "情人节");
gHoliday.Add("0305", "雷锋日");
gHoliday.Add("0308", "妇女节");
gHoliday.Add("03