public string cDate() { ChineseLunisolarCalendar l = new ChineseLunisolarCalendar(); DateTime dt = DateTime.Today; //转换当日的日期 //dt = new DateTime(2006, 1,29);//农历2006年大年初一(测试用),也可指定日期转换 string[] aMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" }; //a10表示日期的十位! string[] a10 = { "初", "十", "廿", "卅" }; string[] aDigi = { "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = ""; int iYear, iMonth, iDay; iYear = l.GetYear(dt); iMonth = l.GetMonth(dt); iDay = l.GetDayOfMonth(dt); //Format Year sYearArab = iYear.ToString(); for (int i = 0; i < sYearArab.Length; i++) { sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))]; } //Format Month int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月 /**/ /*闰月可以出现在一年的任何月份之后。例如,GetMonth 方法返回一个介于 1 到 13 之间的数字来表示与指定日期关联的月份。如果在一年的八月和九月之间有一个闰月,则 GetMonth 方法为八月返回 8,为闰八月返回 9,为九月返回 10。*/ if (iLeapMonth > 0 && iMonth <= iLeapMonth) { //for (int i = iLeapMonth + 1; i < 13; i++) aMonth[i] = aMonth[i - 1]; aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth - 1]; sMonth = aMonth[l.GetMonth(dt)]; } else if (iLeapMonth > 0 && iMonth > iLeapMonth) { sMonth = aMonth[l.GetMonth(dt) - 1]; } else { sMonth = aMonth[l.GetMonth(dt)]; } //Format Day sDay10 = a10[iDay / 10]; sDay1 = aDigi[(iDay % 10)]; sDay = sDay10 + sDay1; if (iDay == 10) sDay = "初十"; if (iDay == 20) sDay = "二十"; if (iDay == 30) sDay = "三十"; //Format Lunar Date //sLuniSolarDate = dt.Year+"年"+dt.Month+"月"+dt.Day+"日 "+Weeks(dt.DayOfWeek.ToString())+" 农历" + sYear + "年" + sMonth + sDay; sLuniSolarDate = dt.Year + "年" + dt.Month + "月" + dt.Day + "日 " + Weeks(dt.DayOfWeek.ToString()) + " 农历" + sMonth + sDay; return sLuniSolarDate; }
/// <summary> /// 公历转为农历的函数 /// </summary> /// <remarks>作者:DeltaCat</remarks> /// <example>网址:http://www.zu14.cn</example> /// <param name="solarDateTime">公历日期</param> /// <returns>农历的日期</returns> static string SolarToChineseLunisolarDate(DateTime solarDateTime) { System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar(); int year = cal.GetYear(solarDateTime); int month = cal.GetMonth(solarDateTime); int day = cal.GetDayOfMonth(solarDateTime); int leapMonth = cal.GetLeapMonth(year); string stringFormat = $"农历" + $"{"甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]}" + $"{"子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]}" + $"({"鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]})年" + $"{(month == leapMonth ? "闰" : "")}{"无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]}月" +
/// <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); if (month > 12) month -= 12; dayOfMonth = cc.GetDayOfMonth(time); isLeap = cc.IsLeapMonth(year, month); if (isLeap) month -= 1; this.time = time; }
/// <summary> /// 返回指定公历日期对应的农历月 /// </summary> /// <param name="calendar"></param> /// <param name="date"></param> /// <returns></returns> public static string GetLunisolarMonth(this ChineseLunisolarCalendar calendar, DateTime date) { var month = calendar.GetMonth(date); var leapMonth = calendar.GetLeapMonth(calendar.GetYear(date), calendar.GetEra(date)); var isleep = leapMonth > 0 && leapMonth == month; if (leapMonth > 0) { if (leapMonth <= month) { month--; } } return(string.Concat(isleep ? "闰" : string.Empty, GetLunisolarMonth(month), "月")); }
// int GetMonth (DateTime time)获取指定公历日期的农历月份 public string GetMonth(DateTime time) { int month = calendar.GetMonth(time); int year = calendar.GetYear(time); int leap = 0; //正月不可能闰月 for (int i = 3; i <= month; i++) { if (calendar.IsLeapMonth(year, i)) { leap = i; break; //一年中最多有一个闰月 } } if (leap > 0) { month--; } return((leap == month + 1 ? "闰" : "") + ChineseMonthName[month - 1]); }
///<summary> ///中国式的时间格式 ///子时:23:00-1:00 ///丑时:1:00-3:00 ///寅时:3:00-5:00 ///卯时:5:00-7:00 ///辰时:7:00-9:00 ///巳时:9:00-11:00 ///午时:11:00-13:00 ///未时:13:00-15:00 ///申时:15:00-17:00 ///酉时:17:00-19:00 ///戌时:19:00-21:00 ///亥时:21:00-23:00 /// </summary> /// <param name="date">时间参数</param> /// <returns></returns> public static string GetChineseDate(DateTime date) { var cnDate = new ChineseLunisolarCalendar(); string[] arrMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月" }; string[] arrDay = { "", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" }; string[] arrYear = { "", "甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉", "甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛己", "壬午", "癸未", "甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸丑", "甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑", "甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥" }; var lYear = cnDate.GetYear(date); var sYear = arrYear[cnDate.GetSexagenaryYear(date)]; var lMonth = cnDate.GetMonth(date); var lDay = cnDate.GetDayOfMonth(date); //获取第几个月是闰月,等于0表示本年无闰月 var leapMonth = cnDate.GetLeapMonth(lYear); var sMonth = arrMonth[lMonth]; //如果今年有闰月 if (leapMonth > 0) { //闰月数等于当前月份 sMonth = lMonth == leapMonth ? string.Format("闰{0}", arrMonth[lMonth - 1]) : sMonth; sMonth = lMonth > leapMonth ? arrMonth[lMonth - 1] : sMonth; } return string.Format("{0}年{1}{2}", sYear, sMonth, arrDay[lDay]); }
//显示农历日期 string GetCNDate() { DateTime m_Date; //今天的日期 int cny; //农历的年月日 int cnm; //农历的年月日 int cnd; //农历的年月日 int icnm; //农历闰月 m_Date = DateTime.Today; ChineseLunisolarCalendar cnCalendar =new ChineseLunisolarCalendar(); cny = cnCalendar.GetSexagenaryYear(m_Date); cnm = cnCalendar.GetMonth(m_Date); cnd = cnCalendar.GetDayOfMonth(m_Date); icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date)); string txcns = "农历"; const string szText1 = "癸甲乙丙丁戊己庚辛壬"; const string szText2 = "亥子丑寅卯辰巳午未申酉戌"; const string szText3 = "猪鼠牛虎免龙蛇马羊猴鸡狗"; int tn = cny % 10; //天干 int dn = cny % 12; //地支 //txcns += szText1.Substring(tn, 1); //txcns += szText2.Substring(dn, 1); //txcns += "(" + szText3.Substring(dn, 1) + ")年"; //格式化月份显示 string[] cnMonth ={ "", "正月", "二月", "三月", "四月", "五月", "六月" , "七月", "八月", "九月", "十月", "十一月", "十二月", "十二月" }; if (icnm > 0) { for (int i = icnm + 1; i < 13; i++) cnMonth[icnm] = cnMonth[icnm - 1]; cnMonth[icnm] = "闰" + cnMonth[icnm]; } txcns += cnMonth[cnm]; string[] cnDay ={ "", "初一", "初二", "初三", "初四", "初五", "初六", "初七" , "初八", "初九", "初十", "十一", "十二", "十三" , "十四", "十五", "十六" , "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五" , "廿六", "廿七", "廿八", "廿九", "三十" }; txcns += cnDay[cnd]; return txcns; }
string GetCNDate() { DateTime m_Date; //��������� int cny; //ũ���������� int cnm; //ũ���������� int cnd; //ũ���������� int icnm; //ũ������ m_Date = DateTime.Today; ChineseLunisolarCalendar cnCalendar = new ChineseLunisolarCalendar(); cny = cnCalendar.GetSexagenaryYear(m_Date); cnm = cnCalendar.GetMonth(m_Date); cnd = cnCalendar.GetDayOfMonth(m_Date); icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date)); string txcns = "ũ��"; const string szText1 = "����ұ����켺������"; const string szText2 = "���ӳ���î������δ������"; const string szText3 = "����ţ������������F��"; int tn = cny % 10; //��� int dn = cny % 12; //��֧ //txcns += szText1.Substring(tn, 1); //txcns += szText2.Substring(dn, 1); //txcns += "(" + szText3.Substring(dn, 1) + ")��"; //��ʽ���·���ʾ string[] cnMonth ={ "", "����", "����", "����", "����", "����", "����" , "����", "����", "����", "ʮ��", "ʮһ��", "ʮ����", "ʮ����" }; if (icnm > 0) { for (int i = icnm + 1; i < 13; i++) cnMonth[icnm] = cnMonth[icnm - 1]; cnMonth[icnm] = "��" + cnMonth[icnm]; } txcns += cnMonth[cnm]; string[] cnDay ={ "", "��һ", "����", "����", "����", "����", "����", "����" , "����", "����", "��ʮ", "ʮһ", "ʮ��", "ʮ��" , "ʮ��", "ʮ��", "ʮ��" , "ʮ��", "ʮ��", "ʮ��", "��ʮ", "إһ", "إ��", "إ��", "إ��", "إ��" , "إ��", "إ��", "إ��", "إ��", "��ʮ" }; txcns += cnDay[cnd]; return txcns; }
/// <summary> /// 获得农历 /// </summary> /// <returns>http://img1.eqingyu.com</returns> public static string cDate(DateTime dt) { ChineseLunisolarCalendar l = new ChineseLunisolarCalendar(); //DateTime dt = DateTime.Today; //转换当日的日期 // // dt = new DateTime(2006, 1,29); //农历2006年大年初一(测试用),也可指定日期转换 string[] aMonth ={ "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" }; //a10表示日期的十位! string[] a10 ={ "初", "十", "廿", "卅" }; string[] aDigi ={ "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = ""; int iYear, iMonth, iDay; iYear = l.GetYear(dt); iMonth = l.GetMonth(dt); iDay = l.GetDayOfMonth(dt); //Format Year sYearArab = iYear.ToString(); for (int i = 0; i < sYearArab.Length; i++) { sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))]; } //Format Month int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月 if (iLeapMonth > 0 && iMonth <= iLeapMonth) { // for (int i = iLeapMonth + 1; i < 13; i++) { aMonth[i] = aMonth[i - 1]; aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth - 1]; sMonth = aMonth[l.GetMonth(dt)]; } } else if (iLeapMonth > 0 && iMonth > iLeapMonth) { sMonth = aMonth[l.GetMonth(dt) - 1]; } else { sMonth = aMonth[l.GetMonth(dt)]; } //Format Day sDay10 = a10[iDay / 10]; sDay1 = aDigi[(iDay % 10)]; sDay = sDay10 + sDay1; if (iDay == 10) sDay = "初十"; if (iDay == 20) sDay = "二十"; if (iDay == 30) sDay = "三十"; //Format Lunar Date // sLuniSolarDate = dt.Year + "年" + dt.Month + "月" + dt.Day + "日 " + Weeks(dt) + " 农历" + sYear + "年" + sMonth + sDay; sLuniSolarDate = "" + sMonth + sDay; return sLuniSolarDate; }