C # Chinese lunar calendar time

Source: Internet
Author: User

You have prepared to write it yourself.AlgorithmAnd study the lunar calendar rules. I found that it was too difficult and troublesome. Just for the calculation of the lunar calendar, I will wait for the people who specialize in calendar to understand it. Later I found out. Net Class Library also provides some basic lunar class system. Globalization. chineselunisolarcalendar. I modified the time format like datetime.CodeAs follows. Converts the Gregorian calendar to the lunar calendar. But it can only be counted as 1900 ~ Between 2100. It is enough for daily use.Source codeAs follows.
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace System
...{
/** // <Summary>
/// Common Chinese calendar date and time
/// </Summary>
Class chinadatetime
...{
Private int year, month, dayofmonth;
Private bool isleap;
Public datetime time;

/** // <Summary>
/// Obtain the calendar year of the current date
/// </Summary>
Public int year
...{
Get... {return year ;}
}

/** // <Summary>
/// Obtain the calendar month of the current date
/// </Summary>
Public int month
...{
Get... {return month ;}
}

/** // <Summary>
/// Obtain the number of days in the current calendar month
/// </Summary>
Public int dayofmonth
...{
Get... {return dayofmonth ;}
}

/** // <Summary>
/// Obtain whether the current date is the date in the leap month
/// </Summary>
Public bool isleap
...{
Get... {return isleap ;}
}

System. Globalization. chineselunisolarcalendar cc;
/** // <Summary>
/// Return the Time of the specified Gregorian Calendar
/// </Summary>
/// <Param name = "time"> </param>
Public chinadatetime (datetime time)
...{
Cc = new system. Globalization. chineselunisolarcalendar ();
If (Time> cc. maxsupporteddatetime | time <cc. minsupporteddatetime)
Throw new exception ("the parameter date and time are not supported. Supported range:" + CC. minsupporteddatetime. toshortdatestring () + "to" + CC. maxsupporteddatetime. toshortdatestring ());
Year = cc. getyear (time );
Month = cc. getmonth (time );
Dayofmonth = cc. getdayofmonth (time );
Isleap = cc. isleapmonth (year, month );
If (isleap) month-= 1;
This. Time = time;

}

/** // <Summary>
/// Return the date of the current day.
/// </Summary>
Public static chinadatetime now
...{
Get... {return New chinadatetime (datetime. Now );}
}

/** // <Summary>
/// Returns whether the specified calendar year, month, or day is the lunar date and time of the leap month.
/// </Summary>
/// <Param name = "year"> </param>
/// <Param name = "month"> </param>
/// <Param name = "dayofmonth"> </param>
/// <Param name = "isleap"> </param>
Public chinadatetime (INT year, int month, int dayofmonth, bool isleap)
...{
If (Year> = cc. maxsupporteddatetime. Year | year <= cc. minsupporteddatetime. Year)
Throw new exception ("the parameter year time is not supported. Supported range:" + CC. minsupporteddatetime. toshortdatestring () + "to" + CC. maxsupporteddatetime. toshortdatestring ());

If (month <1 | month> 12)
Throw new exception ("the month must be 1 ~ 12 range ");
Cc = new system. Globalization. chineselunisolarcalendar ();
If (CC. getleapmonth (year )! = Month & isleap)
Throw new exception ("the specified month is not the leap month of the current year ");
If (CC. getdaysinmonth (year, isleap? Month + 1: month) <dayofmonth | dayofmonth <1)
Throw new exception ("the number of days in the specified month is not within the valid range of days in the current month ");
Year = year;
Month = month;
Dayofmonth = dayofmonth;
Isleap = isleap;
Time = datetime. now;
}

/** // <Summary>
/// Obtain the calendar time of the Current lunar date
/// </Summary>
Public datetime todatetime ()
...{
Return cc. todatetime (year, isleap? Month + 1: Month, dayofmonth, time. Hour, time. Minute, time. Second, time. millisecond );
}

/** //


// obtain the Gregorian calendar time corresponding to the specified Lunar Calendar time
///
///
//
Public static datetime todatetime (chinadatetime cntime)
... {
return cntime. todatetime ();
}

/** // <Summary>
/// Obtain the specified Gregorian calendar time and convert it to the lunar calendar time
/// </Summary>
/// <Param name = "time"> </param>
/// <Returns> </returns>
Public static chinadatetime tochinadatetime (datetime time)
...{
Return new chinadatetime (time );
}

/** // <Summary>

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.