Show today's date JS code (Gregorian and lunar) _javascript tips

Source: Internet
Author: User

1. Display Date Code:

JS Code

/* Get current date/function GetCurrentDateTime () {var d = new Date (); 
var year = D.getfullyear (); 
var month = D.getmonth () + 1; 
var date = D.getdate (); 
var week = D.getday (); 
/* Time seconds * */*var hours = d.gethours (); 
var minutes = d.getminutes (); 
var seconds = d.getseconds (); 
var ms = D.getmilliseconds (); * * var curdatetime = year; 
if (Month > 9) curdatetime = Curdatetime + "year" + month; 
else Curdatetime = curdatetime + "Year 0" + month; 
if (Date > 9) curdatetime = Curdatetime + "Month" + Date + "Day"; 
else Curdatetime = curdatetime + "Month 0" + Date + "Day"; 
/*if (Hours > 9) curdatetime = Curdatetime + "" + hours; 
else Curdatetime = Curdatetime + "0" + hours; 
if (Minutes > 9) curdatetime = Curdatetime + ":" + minutes; 
else Curdatetime = curdatetime + ": 0" + minutes; 
if (Seconds > 9) curdatetime = Curdatetime + ":" + seconds; 
else Curdatetime = curdatetime + ": 0" + seconds;*/var weekday = ""; 
if (week = = 0) weekday = "Sunday"; else if (week = 1) weekday = "Monday";
else if (week = 2) weekday = "Tuesday"; 
else if (week = 3) weekday = "Wednesday"; 
else if (week = 4) weekday = "Thursday"; 
else if (week = 5) weekday = "Friday"; 
else if (week = 6) weekday = "Saturday"; 
Curdatetime = Curdatetime + "" + weekday; 
return curdatetime; }

2. Display the Lunar code:

JS Code

/* Get current Lunar/function showcal () {var d=new Date (); 
var yy=d.getfullyear (); 
var mm=d.getmonth () +1; 
var dd=d.getdate (); 
var ww=d.getday (); 
var ss=parseint (D.gettime ()/1000); 
if (yy<100) yy= "+yy"; 
Return Getlunarday (YY,MM,DD); 
//define global variable var calendardata=new Array (100); 
var madd=new Array (12); 
var tgstring= "A B-n-butyl Xing"; 
var dzstring= "Kaniko Chen has not Shin Hai"; 
var numstring= "1,234,567,890"; 
var monstring= "is 234,567,890 winter wax"; 
var weekstring= "Day 123456"; 
var sx= "Mouse cow tiger rabbit snake horse sheep monkey chicken Dog pig"; 
var cyear,cmonth,cday,thedate; CalendarData = new Array (0XA4B,0X5164B,0X6A5,0X6D4,0X415B5,0X2B6,0X957,0X2092F,0X497,0X60C96,0XD4A,0XEA5,0X50DA9, 0x5ad,0x2b6,0x3126e, 0x92e,0x7192d,0xc95,0xd4a,0x61b4a,0xb55,0x56a,0x4155b, 0x25d,0x92d,0x2192b,0xa95,0x71695, 0x6ca,0xb55,0x50ab5,0x4da,0xa5b,0x30a57,0x52b,0x8152a,0xe95,0x6aa,0x615aa,0xab5,0x4b6,0x414ae,0xa57,0x526, 0x31d26,0xd95,0x70b55,0x56a,0x96d,0x5095d,0x4ad,0xa4d,0x41a4d,0xd25,0x81aa5,0xb54,0xb6a,0x612da,0x95b,0x49b, 0X41497,0XA4B,0XA164B, 0x6a5,0x6d4,0x615b4,0xab6,0X957,0X5092F,0X497,0X64B, 0x30d4a,0xea5,0x80d65,0x5ac,0xab6,0x5126d,0x92e,0xc96,0x41a95,0xd4a,0xda5,0x20b55, 
0X56A,0X7155B,0X25D,0X92D,0X5192B,0XA95,0XB4A,0X416AA,0XAD5,0X90AB5,0X4BA,0XA5B, 0x60A57,0x52B,0xA93,0x40E95); 
madd[0]=0; 
madd[1]=31; 
madd[2]=59; 
madd[3]=90; 
madd[4]=120; 
madd[5]=151; 
madd[6]=181; 
madd[7]=212; 
madd[8]=243; 
madd[9]=273; 
madd[10]=304; 

madd[11]=334; 
function Getbit (m,n) {return (m>>n) &1; ///Lunar transformation function e2c () {thedate= (arguments.length!=3)? New Date (): new Date (arguments[0],arguments[1],arguments[2) 
; 
var total,m,n,k; 
var Isend=false; 
var tmp=thedate.getyear (); 
if (tmp<1900) {tmp+=1900; 

} total= (tmp-1921) *365+math.floor ((tmp-1921)/4) +madd[thedate.getmonth ()]+thedate.getdate ()-38; 
if (Thedate.getyear ()%4==0&&thedate.getmonth () >1) {total++; for (m=0;;; 
m++) {k= (CALENDARDATA[M]&LT;0XFFF)? 11:12; 
for (n=k;n>=0;n--) {if (Total<=29+getbit (calendardata[m],n)) {isend=true; } total=total-29-getbit (CalENDARDATA[M],N); 
} if (isend) break; 
} cyear=1921 + M; 
cmonth=k-n+1; 
Cday=total; 
if (k==12) {if (Cmonth==math.floor (calendardata[m]/0x10000) +1) {cmonth=1-cmonth; 
} if (Cmonth>math.floor (calendardata[m]/0x10000) +1) {cmonth--; 
}} function Getcdatestring () {var tmp= ""; 
/* Show Agricultural calendar year: (such as: Jiawu (MA) years)//*tmp+=tgstring.charat ((cYear-4)%10); 
Tmp+=dzstring.charat ((cYear-4)%12); 
tmp+= "("; 
Tmp+=sx.charat ((cYear-4)%12); 
tmp+= "Year"; */if (cmonth<1) {tmp+= "(leap)"; 
Tmp+=monstring.charat (-cmonth-1); 
}else{Tmp+=monstring.charat (cMonth-1); 
} tmp+= "Month"; tmp+= (cday<11)? " The first ":((cday<20)?" Ten ":((cday<30)?" 
20 ":" 30 ")); if (cday%10!=0| | 
cday==10) {Tmp+=numstring.charat (cDay-1)%10); 
return TMP; } function Getlunarday (solaryear,solarmonth,solarday) {//solaryear = solaryear<1900? ( 
1900+solaryear): solaryear; 
if (solaryear<1921 | | solaryear>2020) {return ""; }else{Solarmonth = (parseint (solarmonth) >0)? 
(solarMonth-1): 11; E2C (Solaryear,solarmonth,solarday);
return getcdatestring (); } 
}

3. Display:

JS Code

$ (function () { 
var date = GetCurrentDateTime (); 
var calendar = showcal (); 
$ ("#currentDate"). Text ("Today is:" + date + "Lunar calendar:" + calendar); 
});

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.