Chineselunisolarcalendar Lunar Date

Source: Internet
Author: User
Tags datetime

In the. net2.0 version of System.Globalization.ChineseLunisolarCalendar is the Chinese calendar class, the Gregorian calendar and the Chinese traditional lunar calendar between the conversion, using it can calculate the celestial stems and earthly branches and other information about the lunar calendar.

Use

static Chineselunisolarcalendar Ccalendar = new Chineselunisolarcalendar ();

Ccalendar.maxsupporteddatetime returns the maximum supported date, that is, 2101-1-28

Ccalendar.minsupporteddatetime returns the minimum supported date, that is, 1901-2-19

Here we will implement the Gregorian calendar to the lunar calendar.

/// <summary>
/// 根据公历获取农历日期
/// </summary>
/// <param name="datetime">公历日期</param>
/// <returns></returns>
public static string GetChineseDateTime(DateTime datetime)
{
     int lyear = cCalendar.GetYear(datetime);
     int lmonth = cCalendar.GetMonth(datetime);
     int lday = cCalendar.GetDayOfMonth(datetime);
     //获取闰月, 0 则表示没有闰月
     int leapMonth = cCalendar.GetLeapMonth(lyear);
     bool isleap = false;
     if (leapMonth > 0)
     {
         if (leapMonth == lmonth)
         {
             //闰月
             isleap = true;
             lmonth--;
         }
         else if (lmonth > leapMonth)
         {
             lmonth--;
         }
     }
     return string.Concat(GetLunisolarYear(lyear), "年", isleap ? " 闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay (lday));
}

Results of the test:

Incoming Date: 2010-3-4

Return to the Lunar New Year: the first month of 19

Can satisfy the simple demand.

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.