C # convert the Gregorian calendar to the lunar calendar

Source: Internet
Author: User

In. system. globalization. chineseLunisolarCalendar is a calendar class for China. It is used to calculate information about the calendar, such as the calendar and calendar year of the traditional Chinese calendar.

Use

static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
CCalendar. MaxSupportedDateTime returns the supported maximum date, that is, 2101-1-28
CCalendar. MinSupportedDateTime returns the supported minimum date, that is
 
Next we will convert the Gregorian calendar to the lunar calendar.
/// <Summary> /// obtain the lunar date based on the Gregorian calendar /// </summary> /// <param name = "datetime"> Gregorian date </param> // /<returns> </returns> public static string GetChineseDateTime (DateTime datetime) {int lyear = cCalendar. getYear (datetime); int lmonth = cCalendar. getMonth (datetime); int lday = cCalendar. getDayOfMonth (datetime); // get the leap month. If the value is 0, the leapMonth int leapMonth = cCalendar is not displayed. getLeapMonth (lyear); bool isleap = false; if (leapMonth> 0) {I F (leapMonth = lmonth) {// leap = true; lmonth --;} else if (lmonth> leapMonth) {lmonth --;} return string. concat (GetLunisolarYear (lyear), "year", isleap? "Interval": string. Empty, GetLunisolarMonth (lmonth), "month", GetLunisolarDay (lday ));
}

Test results:

Input Date: 2010-3-4

Return to lunar calendar: Geng Yin, September 1, lunar January 19

It can meet simple requirements.

Other code is also attached:

# Region agricultural calendar year // <summary> // ten-day dry // </summary> private static string [] tiangan = {"A", "B ", "C", "ding", "E", "Ji", "Geng", "Xin", "Yi", "Xi "}; /// <summary> /// 12th branch // </summary> private static string [] dizhi = {"sub", "ugly", "", "Mao", "Chen", "Si", "Wu", "wei", "shen", "", "Wu", "Hai "}; /// <summary> /// Zodiac /// </summary> private static string [] shengxiao = {"rat", "Ox", "Tiger ", "free", "dragon", "snake", "horse", "goat", "monkey", "chicken", "dog", "Swine "}; /// < Summary> /// return the year of the calendar month. // </summary> // <param name = "year"> calendar year </param> // <returns> </returns> public static string GetLunisolarYear (int year) {if (year> 3) {int tgIndex = (year-4) % 10; int dzIndex = (year-4) % 12; return string. concat (tiangan [tgIndex], dizhi [dzIndex], "[", shengxiao [dzIndex], "]");} throw new ArgumentOutOfRangeException ("invalid year! ") ;}# Endregion

# Region lunar month // <summary> // lunar month // </summary> private static string [] months = {"zheng", "2 ", "3", "4", "5", "6", "7", "8", "9", "10", "11 ", "Twelve (wax )"}; /// <summary> /// returns the lunar month /// </summary> /// <param name = "month"> month </param> /// <returns> </returns> public static string GetLunisolarMonth (int month) {if (month <13 & month> 0) {return months [month-1];} throw new ArgumentOutOfRangeException ("invalid month! ") ;}# Endregion

# Region lunar day // <summary> //// </summary> private static string [] days1 = {"", "10", "Hangzhou ", "3"}; // <summary> // day // </summary> private static string [] days = {"1", "2 ", "3", "4", "5", "6", "7", "8", "9", "10 "}; /// <summary> /// return to the lunar calendar day /// </summary> /// <param name = "day"> </param> /// <returns> </returns> public static string GetLunisolarDay (int day) {if (day> 0 & day <32) {if (day! = 20 & day! = 30) {return string. concat (days1 [(day-1)/10], days [(day-1) % 10]);} else {return string. concat (days [(day-1)/10], days1 [1]) ;}} throw new ArgumentOutOfRangeException ("invalid day! ") ;}# Endregion

There is also a code to get the Zodiac by date:

/// <Summary> /// returns the Chinese zodiac // </summary> /// <param name = "datetime"> calendar date </param> /// <returns> </returns> public static string GetShengXiao (DateTime datetime) {return shengxiao [cCalendar. getTerrestrialBranch (cCalendar. getSexagenaryYear (datetime)-1];}
 source:http://www.cnblogs.com/huxj/archive/2010/03/04/1678160.html

Read the full text

Category:Net view comments

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.