C # lunar calendar

Source: Internet
Author: User

There are already a lot of C # lunar classes on the Internet, but it seems complicated. In fact, C # already provides ChineseLunisolarCalendar for Chinese lunar classes. However, it does not provide solar term and holiday functions.

According to the method byte on the Internet, a Chinese lunar calendar class is written!

ChinaDate. GetChinaDate (DateTime dt) to obtain the lunar date

ChinaDate. GetYear (DateTime dt) to obtain the year of the lunar calendar (including day-to-day support and Zodiac Information)

ChinaDate. GetMonth (DateTime dt) to obtain the lunar month

ChinaDate. GetDay (DateTime dt) to obtain the lunar date

ChinaDate. GetSolarTerm (DateTime dt) Get the Solar Term

ChinaDate. GetHoliday (DateTime dt) Get calendar holidays

ChinaDate. GetChinaHoliday (DateTime dt) Get the lunar calendar Festival

Using System. globalization; using System. collections; using System; // <summary> // Chinese lunar calendar // </summary> // Date: 2011-01-13 // Author: http://www.cnblogs.com/zjfree/public static class ChinaDate {private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar (); private static Hashtable gHoliday = new Hashtable (); private static Hashtable nHoliday = new Hashtable (); private static string [] JQ = {"Xiao Han "," Big cold "," Spring "," Rain "," stunned "," Spring Equinox "," Qingming "," Gu Yu "," lixia "," xiaoman ", "Mango", "", "White Dew", "Autumn Equinox", "Cold Dew", "Frost fall ", "Winter", "snow", "snow", "Winter Solstice"}; private static int [] JQData = {0, 21208,434 67, 63836,853 37, 107014,128 867, 150921,173 149, 195551,218 072, 240693,263 343, 285989,308 563, 331033,353 350, 375494,397 447, 419210,440 795, 462224,483 532, 504758}; static ChinaDate () {// calendar gHoliday. add ("010 1 "," New Year's Day "); gHoliday. add ("0214", "Valentine's Day"); gHoliday. add ("0305", "Lei Feng Day"); gHoliday. add ("0308", "Women's Day"); gHoliday. add ("0312", "Arbor Day"); gHoliday. add ("0315", "Consumer equity Day"); gHoliday. add ("0401", "Fool's Day"); gHoliday. add ("0501", "Labor Day"); gHoliday. add ("0504", "Youth Day"); gHoliday. add ("0601", "Children's Day"); gHoliday. add ("0701", ""); gHoliday. add ("0801", "Jianjun Festival"); gHoliday. add ("0910", "Teacher's Day"); gHoliday. add ("1001", "National Day"); gHoliday. add ("1224", "Ping "); GHoliday. add ("1225", "Christmas"); // nHoliday of the lunar calendar. add ("0101", "Spring Festival"); nHoliday. add ("0115", "Lantern Festival"); nHoliday. add ("0505", "Dragon Boat Festival"); nHoliday. add ("0815", "Mid-Autumn Festival"); nHoliday. add ("0909", "Double Ninth Festival"); nHoliday. add ("1208", "LABA Festival ");} /// <summary> /// obtain the lunar calendar // </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetChinaDate (DateTime dt) {if (dt> china. maxSupportedDateTime | dt <china. minSuppor TedDateTime) {// Date range: February 19-21, 1901-January 28 throw new Exception (string. Format ("date out of range! It must be between {0} and {1! ", China. minSupportedDateTime. toString ("yyyy-MM-dd"), china. maxSupportedDateTime. toString ("yyyy-MM-dd");} string str = string. format ("{0} {1} {2}", GetYear (dt), GetMonth (dt), GetDay (dt); string strJQ = GetSolarTerm (dt ); if (strJQ! = "") {Str + = "(" + strJQ + ")" ;}string strHoliday = GetHoliday (dt); if (strHoliday! = "") {Str + = "" + strHoliday;} string strChinaHoliday = GetChinaHoliday (dt); if (strChinaHoliday! = "") {Str + = "" + strChinaHoliday;} return str ;} /// <summary> /// obtain the year of the lunar calendar /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetYear (DateTime dt) {int yearIndex = china. getSexagenaryYear (dt); string yearTG = "A, B, C, E, E, and z"; string yearDZ = "; string yearSX = "rat, ox, Tiger, Rabbit, dragon, Snake, horse, monkey, chicken, dog, and pig"; int year = china. getYear (dt); int yTG = china. getCelestialStem (yearIndex); int yDZ = c Hina. getTerrestrialBranch (yearIndex); string str = string. format ("[{1}] {2} {3} {0}", year, yearSX [yDZ], yearTG [yTG], yearDZ [yDZ]); return str ;} /// <summary> /// obtain the lunar month /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetMonth (DateTime dt) {int year = china. getYear (dt); int iMonth = china. getMonth (dt); int leapMonth = china. getLeapMonth (year); bool isLeapMonth = iMonth = = LeapMonth; if (leapMonth! = 0 & iMonth> = leapMonth) {iMonth --;} string szText = "2345 80 or 90"; string strMonth = isLeapMonth? "Expiration": ""; if (iMonth <= 10) {strMonth + = szText. substring (iMonth-1, 1);} else if (iMonth = 11) {strMonth + = "11";} else {strMonth + = "La ";} return strMonth + "month ";} /// <summary> /// obtain the lunar date /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetDay (DateTime dt) {int iDay = china. getDayOfMonth (dt); string szText1 = ""; string szText2 = "February 1234 80 or 90"; string strDay; if (iDay = 20) {strDay = "20";} else if (iDay = 30) {strDay = "30";} else {strDay = szText1.Substring (iDay-1) /10, 1); strDay = strDay + szText2.Substring (iDay-1) % 10, 1);} return strDay ;} /// <summary> /// get the solar term /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetSolarTerm (DateTime dt) {DateTime dtBase = new DateTime (1900, 1, 6, 2, 5, 0); DateTime dtNew; double num; int y; String strReturn = ""; y = dt. year; for (int I = 1; I <= 24; I ++) {num = 525948.76 * (y-1900) + JQData [I-1]; dtNew = dtBase. addMinutes (num); if (dtNew. dayOfYear = dt. dayOfYear) {strReturn = JQ [I-1] ;}} return strReturn ;} /// <summary> /// get a calendar holiday /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetHoliday (DateTime dt) {string strReturn = ""; object g = gHoliday [dt. mo Nth. ToString ("00") + dt. Day. ToString ("00")]; if (g! = Null) {strReturn = g. toString ();} return strReturn ;} /// <summary> /// obtain the lunar calendar festival /// </summary> /// <param name = "dt"> </param> /// <returns> </returns> public static string GetChinaHoliday (DateTime dt) {string strReturn = ""; int year = china. getYear (dt); int iMonth = china. getMonth (dt); int leapMonth = china. getLeapMonth (year); int iDay = china. getDayOfMonth (dt); if (china. getDayOfYear (dt) = china. getDaysInYear (y Ear) {strReturn = "New Year's Eve";} else if (leapMonth! = IMonth) {if (leapMonth! = 0 & iMonth> = leapMonth) {iMonth --;} object n = nHoliday [iMonth. toString ("00") + iDay. toString ("00")]; if (n! = Null) {if (strReturn = "") {strReturn = n. toString ();} else {strReturn + = "" + n. toString () ;}}return strReturn ;}}

Class Code download: http://files.cnblogs.com/zjfree/ChinaDate.rar

Correction record:

A leap month error occurred while obtaining the lunar month on. Thanks for the tlptotop reminder.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.