JavaScript code for querying "Today's lunar day"

Source: Internet
Author: User

The following JS class is re-designed using the object-oriented idea, making queries today faster than the day of the lunar calendar.

 

Function chinesecalendar (dateobj ){
This. dateobj = (dateobj! = Undefined )? Dateobj: new date ();
This. Sy = This. dateobj. getfullyear ();
This. Sm = This. dateobj. getmonth ();
This. SD = This. dateobj. getdate ();
This. lunarinfo = [0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,
0x14b63];

// Returns the month 1-12 in the Y-year period of the lunar calendar. If no value is returned, the return value is 0.
This. leapmonth = function (y ){
Return this. lunarinfo [Y-1900] & 0xf;
};
// Return the total number of days of the lunar month y
This. monthdays = function (Y, m ){
Return (this. lunarinfo [Y-1900] & (0x10000> m ))? 30: 29;
};
// Returns the number of days in the Y-year month of the Chinese calendar.
This. leapdays = function (y ){
If (this. leapmonth (y )){
Return (this. lunarinfo [Y-1900] & 0x10000 )? 30: 29;
}
Else {
Return 0;
}
};
// Returns the total number of days of the lunar y year.
This. lyeardays = function (y ){
VaR I, sum = 348;
For (I = 0x8000; I> 0x8; I >>= 1 ){
Sum + = (this. lunarinfo [Y-1900] & I )? 1: 0;
}
Return sum + this. leapdays (y );
};
// Calculate the lunar calendar, input the date object, and return the object of the lunar calendar date
// Attributes of this object include. year. Month. Day. isleap. yearcyl. daycyl. moncyl
This. Lunar = function (dateobj ){
VaR I, leap = 0, temp = 0, lunarobj = {};
VaR basedate = new date (1900, 0, 31 );
VaR offset = (dateobj-base date)/86400000;
Lunarobj. daycyl = offset + 40;
Lunarobj. moncyl = 14;
For (I = 1900; I <2050 & Offset> 0; I ++ ){
Temp = This. lyeardays (I );
Offset-= temp;
Lunarobj. moncyl + = 12;
}
If (offset <0 ){
Offset + = temp;
I --;
Lunarobj. moncyl-= 12;
}

Lunarobj. Year = I;
Lunarobj. yearcyl = I-1864;
Leap = This. leapmonth (I );
Lunarobj. isleap = false;
For (I = 1; I <13 & Offset> 0; I ++ ){
If (LEAP> 0 & I = (LEAP + 1) & lunarobj. isleap = false ){
-- I;
Lunarobj. isleap = true;
Temp = This. leapdays (lunarobj. year );
}
Else {
Temp = This. monthdays (lunarobj. Year, I)
}
If (lunarobj. isleap = true & I = (LEAP + 1 )){
Lunarobj. isleap = false;
}
Offset-= temp;
If (lunarobj. isleap = false ){
Lunarobj. moncyl ++;
}
}

If (offset = 0 & leap> 0 & I = leap + 1 ){
If (lunarobj. isleap ){
Lunarobj. isleap = false;
}
Else {
Lunarobj. isleap = true;
-- I;
-- Lunarobj. moncyl;
}
}

If (offset <0 ){
Offset + = temp;
-- I;
-- Lunarobj. moncyl
}
Lunarobj. month = I;
Lunarobj. Day = offset + 1;
Return lunarobj;
};
// Chinese Date
This. CDAY = function (M, d ){
VaR nstr1 = ['day', 'yi', '2', '3', '4', '5', '6', '7', '8 ', '9', '10'];
VaR nstr2 = ['chu', '10', 'hangzhou', 'hangzhou', ''];
VaR S;
If (M> 10 ){
S = '10' + nstr1 [M-10];
}
Else {
S = nstr1 [m];
}
S + = 'month ';
Switch (d ){
Case 10:
S + = '10 ';
Break;
Case 20:
S + = '20 ';
Break;
Case 30:
S + = '30 ';
Break;
Default:
S + = nstr2 [math. Floor (D/10)];
S + = nstr1 [d % 10];
}
Return S;
};
This. solarday2 = function (){
VaR sdobj = new date (this. Sy, this. Sm, this. SD );
VaR ldobj = This. Lunar (sdobj );
VaR TT = 'lunar calendar '+ this. CDAY (ldobj. Month, ldobj. Day );
Ldobj = NULL;
Return tt;
};
This. weekday = function (){
VaR day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Return day [This. dateobj. getday ()];
};
This. yymmdd = function (){
VaR datearr = [This. Sy, 'Year', this. sm + 1, 'month', this. SD, 'day'];
Return datearr. Join ('');
}
}

Usage:

<HTML>
<Head>
<Title>
Lunar Calendar Test
</Title>
<SCRIPT type = "text/JavaScript" src = "cc. js"> </SCRIPT>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
VaR date = new chinesecalendar ();
Document. Write ("Lunar calendar:" + date. solarday2 ());

Document. Write ("Week:" + date. weekday ());

Document. Write ("Date:" + date. yymmdd ());

</SCRIPT>
</Body>
</Html>

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.