利用Javascript获取当前日期的农历日期

来源:http://www.ido321.com/926.html

JavaScript代码

   1: /*设置农历日期*/
   2: var CalendarData=new Array(100);
   3: var madd=new Array(12);
   4: var numString="一二三四五六七八九十";
   5: var monString="正二三四五六七八九十冬腊";

6: var cYear,cMonth,cDay,TheDate;

// 农历每月只能是29或30天,一年用12(或13)个二进制位表示,从高到低,对应位为1表示30天,否则29天

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

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

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

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

0x6A5,0x6D4,0x615B4,0xAB6,0×957,0x5092F,0×497,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);

 

   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+="(闰)";
  64: tmp+=monString.charAt(-cMonth-1);
  65: }else{
  66:  tmp+=monString.charAt(cMonth-1);
  67: }
  68: tmp+="月";
  69: tmp+=(cDay<11)?"初":((cDay<20)?"十":((cDay<30)?"廿":"三十"));
  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: /*农历设置end*/

调用

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

效果:

九月十一就是当天的农历日期

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。