Use Javascript to obtain the lunar date of the current date.

Source: Internet
Author: User

Use Javascript to obtain the lunar date of the current date.

Source: http://www.ido321.com/926.html

JavaScript code

1:/* set the lunar date */
   2: var CalendarData=new Array(100);
   3: var madd=new Array(12);
4: var numString = "August 5, 1234, 80 or 90 ";
5: var monString = "June 80 or 90 Winter ";

6: var cYear, cMonth, cDay, TheDate;

// The lunar calendar month can only be 29 or 30 days. 12 (or 13) binary digits are used for a year. from high to low, 1 indicates 30 days. Otherwise, 29 days are used.

7: CalendarData = new Array (0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x0000b5, 0x2B6, 0 × 957, 0x2092F, 0 × 497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD,

0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x00005b, 0x25D, 0x92D, 0x2192B, 0xA95, 0 × 71695, 0x6CA,

0xB55, fingerprint, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, expires, 0xA57, 0 × 526, 0x31D26, 0xD95,

Primary, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, primary, 0x95B, 0x49B, 0 × 41497, 0xA4B, 0xA164B,

0x6A5, 0x6D4, 0x615B4, 0xAB6, 0 × 957, 0x5092F, 0 × 497, 0x64B, primary, 0xEA5, primary, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95,

0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x4BA, 0xA5B, 0x60A57, 0x52B,

0xA93, 0x40E95 );

 

   8: madd[0]=0;
   9: madd[1]=31;
  10: madd[2]=59;
  11: madd[3]=90;
  12: madd[4]=120;
  13: madd[5]=151;
  14: madd[6]=181;
  15: madd[7]=212;
  16: madd[8]=243;
  17: madd[9]=273;
  18: madd[10]=304;
  19: madd[11]=334;
  20:
  21: function GetBit(m,n){
  22: return (m>>n)&1;
  23: }
  24: function e2c(){
  25: TheDate= (arguments.length!=3) ? new Date() : new Date(arguments[0],arguments[1],arguments[2]);
  26: var total,m,n,k;
  27: var isEnd=false;
  28: var tmp=TheDate.getYear();
  29: if(tmp<1900){
  30:  tmp+=1900;
  31: }
  32: total=(tmp-1921)*365+Math.floor((tmp-1921)/4)+madd[TheDate.getMonth()]+TheDate.getDate()-38;
  33:
  34: if(TheDate.getYear()%4==0&&TheDate.getMonth()>1) {
  35:  total++;
  36: }
  37: for(m=0;;m++){
  38:  k=(CalendarData[m]<0xfff)?11:12;
  39:  for(n=k;n>=0;n--){
  40: if(total<=29+GetBit(CalendarData[m],n)){
  41:  isEnd=true; break;
  42:  }
  43:  total=total-29-GetBit(CalendarData[m],n);
  44:  }
  45:  if(isEnd) break;
  46: }
  47: cYear=1921 + m;
  48: cMonth=k-n+1;
  49: cDay=total;
  50: if(k==12){
  51:  if(cMonth==Math.floor(CalendarData[m]/0x10000)+1){
  52:  cMonth=1-cMonth;
  53:  }
  54: if(cMonth>Math.floor(CalendarData[m]/0x10000)+1){
  55:  cMonth--;
  56:  }
  57: }
  58: }
  59:
  60: function GetcDateString(){
  61: var tmp="";
  62: if(cMonth<1){
63: tmp + = "(bytes )";
  64: tmp+=monString.charAt(-cMonth-1);
  65: }else{
  66:  tmp+=monString.charAt(cMonth-1);
  67: }
68: tmp + = "month ";
69: tmp + = (cDay <11 )? "Chu" :( (cDay <20 )? "10" :( (cDay <30 )? "Identifier": "Thirty "));
  70: if (cDay%10!=0||cDay==10){
  71:  tmp+=numString.charAt((cDay-1)%10);
  72: }
  73: return tmp;
  74: }
  75:
  76: function GetLunarDay(solarYear,solarMonth,solarDay){
  77: //solarYear = solarYear<1900?(1900+solarYear):solarYear;
  78: if(solarYear<1921 || solarYear>2020){
  79: return "";
  80: }else{
  81:  solarMonth = (parseInt(solarMonth)>0) ? (solarMonth-1) : 11;
  82:  e2c(solarYear,solarMonth,solarDay);
  83:  return GetcDateString();
  84: }
  85: }
  86:
  87: var D=new Date();
  88: var yy=D.getFullYear();
  89: var mm=D.getMonth()+1;
  90: var dd=D.getDate();
  91: var ww=D.getDay();
  92: var ss=parseInt(D.getTime() / 1000);
  93: if (yy<100) yy="19"+yy;
  94: function showCal(){
  95: var nongli = GetLunarDay(yy,mm,dd);
  96: return nongli;
  97: }
98:/* Set end for the lunar calendar */

Call

   1: $('#tianqi h5').text(showCal());

Effect:

September 11 is the lunar date of the day.


How to use javascript to obtain the date of the previous month before the current date

<Script>
Var myDate = new Date ();
Alert (myDate. getFullYear () + "-" + (myDate. getMonth () + 1) + "-" + myDate. getDate ());
MyDate. setMonth (myDate. getMonth ()-1 );
Alert (myDate. getFullYear () + "-" + (myDate. getMonth () + 1) + "-" + myDate. getDate ());
</Script>

Javascript retrieves the date of the day, calculates the week of the Day, and lists all the dates from Monday to Sunday of the week.

As you said, there is no problem. Var date = new Date (); date. setDate (date. getDate ()-1) will automatically change the month year, so you don't have to worry about cross-month and cross-year.
For example, var date = new Date (); date. setDate (1 );
Now it is 2013.3.1, date. setDate (date. getDate ()-1 );
Now it is 2013.2.28

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.