显示新历、农历、星期、节气等详细日期信息的代码
显示新历、农历、星期、节气等详细日期信息的代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 5 <title>显示新历、农历、星期、节气等详细日期信息的代码 - 分享JavaScript-sharejs.com</title> 6 <script language="javascript" type="text/javascript"> 7 // JavaScript Document 8 function RunGLNL(){ 9 var today=new Date(); 10 var d=new Array("周日","周一","周二","周三","周四","周五","周六"); 11 var DDDD= d[today.getDay()]; 12 DDDD = DDDD+ " " + (CnDateofDateStr(today)); //显示农历 13 DDDD = DDDD+SolarTerm(today); //显示二十四节气 14 document.write(DDDD); 15 } 16 function DaysNumberofDate(DateGL){ 17 return parseInt((Date.parse(DateGL)-Date.parse(DateGL.getFullYear()+"/1/1"))/86400000)+1; 18 } 19 function CnDateofDate(DateGL){ 20 var CnData=new Array( 21 0x16,0x2a,0xda,0x00,0x83,0x49,0xb6,0x05,0x0e,0x64,0xbb,0x00,0x19,0xb2,0x5b,0x00, 22 0x87,0x6a,0x57,0x04,0x12,0x75,0x2b,0x00,0x1d,0xb6,0x95,0x00,0x8a,0xad,0x55,0x02, 23 0x15,0x55,0xaa,0x00,0x82,0x55,0x6c,0x07,0x0d,0xc9,0x76,0x00,0x17,0x64,0xb7,0x00, 24 0x86,0xe4,0xae,0x05,0x11,0xea,0x56,0x00,0x1b,0x6d,0x2a,0x00,0x88,0x5a,0xaa,0x04, 25 0x14,0xad,0x55,0x00,0x81,0xaa,0xd5,0x09,0x0b,0x52,0xea,0x00,0x16,0xa9,0x6d,0x00, 26 0x84,0xa9,0x5d,0x06,0x0f,0xd4,0xae,0x00,0x1a,0xea,0x4d,0x00,0x87,0xba,0x55,0x04 27 ); 28 var CnMonth=new Array(); 29 var CnMonthDays=new Array(); 30 var CnBeginDay; 31 var LeapMonth; 32 var Bytes=new Array(); 33 var I; 34 var CnMonthData; 35 var DaysCount; 36 var CnDaysCount; 37 var ResultMonth; 38 var ResultDay; 39 var yyyy=DateGL.getFullYear(); 40 var mm=DateGL.getMonth()+1; 41 var dd=DateGL.getDate(); 42 if(yyyy<100) yyyy+=1900; 43 if ((yyyy < 1997) || (yyyy > 2020)){ 44 return 0; 45 } 46 Bytes[0] = CnData[(yyyy - 1997) * 4]; 47 Bytes[1] = CnData[(yyyy - 1997) * 4 + 1]; 48 Bytes[2] = CnData[(yyyy - 1997) * 4 + 2]; 49 Bytes[3] = CnData[(yyyy - 1997) * 4 + 3]; 50 if ((Bytes[0] & 0x80) != 0) {CnMonth[0] = 12;} 51 else {CnMonth[0] = 11;} 52 CnBeginDay = (Bytes[0] & 0x7f); 53 CnMonthData = Bytes[1]; 54 CnMonthData = CnMonthData << 8; 55 CnMonthData = CnMonthData | Bytes[2]; 56 LeapMonth = Bytes[3]; 57 for (I=15;I>=0;I--){ 58 CnMonthDays[15 - I] = 29; 59 if (((1 << I) & CnMonthData) != 0 ){ 60 CnMonthDays[15 - I]++;} 61 if (CnMonth[15 - I] == LeapMonth ){ 62 CnMonth[15 - I + 1] = - LeapMonth;} 63 else{ 64 if (CnMonth[15 - I] < 0 ){CnMonth[15 - I + 1] = - CnMonth[15 - I] + 1;} 65 else {CnMonth[15 - I + 1] = CnMonth[15 - I] + 1;} 66 if (CnMonth[15 - I + 1] > 12 ){ CnMonth[15 - I + 1] = 1;} 67 } 68 } 69 DaysCount = DaysNumberofDate(DateGL) - 1; 70 if (DaysCount <= (CnMonthDays[0] - CnBeginDay)){ 71 if ((yyyy > 1901) && (CnDateofDate(new Date((yyyy - 1)+"/12/31")) < 0)){ 72 ResultMonth = - CnMonth[0];} 73 else {ResultMonth = CnMonth[0];} 74 ResultDay = CnBeginDay + DaysCount; 75 } 76 else{ 77 CnDaysCount = CnMonthDays[0] - CnBeginDay; 78 I = 1; 79 while ((CnDaysCount < DaysCount) && (CnDaysCount + CnMonthDays[I] < DaysCount)){ 80 CnDaysCount+= CnMonthDays[I]; 81 I++; 82 } 83 ResultMonth = CnMonth[I]; 84 ResultDay = DaysCount - CnDaysCount; 85 } 86 if (ResultMonth > 0){ 87 return ResultMonth * 100 + ResultDay;} 88 else{return ResultMonth * 100 - ResultDay;} 89 } 90 function CnYearofDate(DateGL){ 91 var YYYY=DateGL.getFullYear(); 92 var MM=DateGL.getMonth()+1; 93 var CnMM=parseInt(Math.abs(CnDateofDate(DateGL))/100); 94 if(YYYY<100) YYYY+=1900; 95 if(CnMM>MM) YYYY--; 96 YYYY-=1864; 97 return CnEra(YYYY)+"年"; 98 } 99 function CnMonthofDate(DateGL){ 100 var CnMonthStr=new Array("零","正","二","三","四","五","六","七","八","九","十","冬","腊"); 101 var Month; 102 Month = parseInt(CnDateofDate(DateGL)/100); 103 if (Month < 0){return "闰" + CnMonthStr[-Month] + "月";} 104 else{return CnMonthStr[Month] + "月";} 105 } 106 function CnDayofDate(DateGL){ 107 var CnDayStr=new Array("零", 108 "初一", "初二", "初三", "初四", "初五", 109 "初六", "初七", "初八", "初九", "初十", 110 "十一", "十二", "十三", "十四", "十五", 111 "十六", "十七", "十八", "十九", "二十", 112 "廿一", "廿二", "廿三", "廿四", "廿五", 113 "廿六", "廿七", "廿八", "廿九", "三十"); 114 var Day; 115 Day = (Math.abs(CnDateofDate(DateGL)))%100; 116 return CnDayStr[Day]; 117 } 118 function DaysNumberofMonth(DateGL){ 119 var MM1=DateGL.getFullYear(); 120 MM1<100 ? MM1+=1900:MM1; 121 var MM2=MM1; 122 MM1+="/"+(DateGL.getMonth()+1); 123 MM2+="/"+(DateGL.getMonth()+2); 124 MM1+="/1"; 125 MM2+="/1"; 126 return parseInt((Date.parse(MM2)-Date.parse(MM1))/86400000); 127 } 128 function CnEra(YYYY){ 129 var Tiangan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); 130 //var Dizhi=new Array("子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)","巳(蛇)", 131 //"午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"); 132 var Dizhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"); 133 return Tiangan[YYYY%10]+Dizhi[YYYY%12]; 134 } 135 function CnDateofDateStr(DateGL){ 136 if(CnMonthofDate(DateGL)=="零月") return " 请调整您的计算机日期!"; 137 else return "农历"+CnYearofDate(DateGL)+ " " + CnMonthofDate(DateGL) + CnDayofDate(DateGL); 138 } 139 function SolarTerm(DateGL){ 140 var SolarTermStr=new Array( 141 "小寒","大寒","立春","雨水","惊蛰","春分", 142 "清明","谷雨","立夏","小满","芒种","夏至", 143 "小暑","大暑","立秋","处暑","白露","秋分", 144 "寒露","霜降","立冬","小雪","大雪","冬至"); 145 var DifferenceInMonth=new Array( 146 1272060,1275495,1281180,1289445,1299225,1310355, 147 1321560,1333035,1342770,1350855,1356420,1359045, 148 1358580,1355055,1348695,1340040,1329630,1318455, 149 1306935,1297380,1286865,1277730,1274550,1271556); 150 var DifferenceInYear=31556926; 151 var BeginTime=new Date(1901/1/1); 152 BeginTime.setTime(947120460000); 153 for(;DateGL.getFullYear()<BeginTime.getFullYear();){ 154 BeginTime.setTime(BeginTime.getTime()-DifferenceInYear*1000); 155 } 156 for(;DateGL.getFullYear()>BeginTime.getFullYear();){ 157 BeginTime.setTime(BeginTime.getTime()+DifferenceInYear*1000); 158 } 159 for(var M=0;DateGL.getMonth()>BeginTime.getMonth();M++){ 160 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 161 } 162 if(DateGL.getDate()>BeginTime.getDate()){ 163 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 164 M++; 165 } 166 if(DateGL.getDate()>BeginTime.getDate()){ 167 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 168 M==23?M=0:M++; 169 } 170 var JQ="二十四节气"; 171 if(DateGL.getDate()==BeginTime.getDate()){ 172 JQ+=" 今日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 173 } 174 else if(DateGL.getDate()==BeginTime.getDate()-1){ 175 JQ+=" 明日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 176 } 177 else if(DateGL.getDate()==BeginTime.getDate()-2){ 178 JQ+=" 后日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 179 } 180 else{ 181 JQ=" 二十四节气"; 182 if(DateGL.getMonth()==BeginTime.getMonth()){ 183 JQ+=" 本月"; 184 } 185 else{ 186 JQ+=" 下月"; 187 } 188 JQ+=BeginTime.getDate()+"日"+"<font color='#598F03'><b>"+SolarTermStr[M]+"</b></font>"; 189 } 190 return JQ; 191 } 192 </script> 193 </head> 194 <body> 195 <script language="JavaScript"> 196 RunGLNL(); 197 </script> 198 199 <br><br> 200 <div align="center"> 201 </div> 202 </body> 203 </html>