用js显示当前时间,节气,农历
首先这个脚本是在网上找的,但是不完整,我只是稍微整理了一下,实现自己想要的效果,后期还会加上某地区上的天气情况。
上面这张图片就是实现后的效果!
Time
1 <script language="javascript" type="text/javascript"> 2 // JavaScript Document 3 function RunGLNL(){ 4 var today=new Date(); 5 var d=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); 6 var DDDD=today.getFullYear()+"年"+[today.getMonth()+1]+"月"+today.getDate()+"日"; 7 intHours = today.getHours(); 8 intMinutes = today.getMinutes(); 9 intSeconds = today.getSeconds(); 10 if (intHours == 0) { 11 hours = "12:"; 12 xfile = "午夜"; 13 } else if (intHours < 12) { 14 hours = intHours+":"; 15 xfile = "上午"; 16 } else if (intHours == 12) { 17 hours = "12:"; 18 xfile = "正午"; 19 } else { 20 intHours = intHours - 12 21 hours = intHours + ":"; 22 xfile = "下午"; 23 } 24 if (intMinutes < 10) { 25 minutes = "0"+intMinutes+":"; 26 } else { 27 minutes = intMinutes+":"; 28 } 29 if (intSeconds < 10) { 30 seconds = "0"+intSeconds+" "; 31 } else { 32 seconds = intSeconds+" "; 33 } 34 DDDD=DDDD+xfile+hours+minutes+seconds; 35 DDDD=DDDD+" "+ d[today.getDay()]; 36 var DDDD = DDDD+ " " + (CnDateofDateStr(today)); //显示农历 37 DDDD = DDDD+SolarTerm(today); //显示二十四节气 38 document.write(DDDD); 39 } 40 41 function DaysNumberofDate(DateGL){ 42 return parseInt((Date.parse(DateGL)-Date.parse(DateGL.getFullYear()+"/1/1"))/86400000)+1; 43 } 44 function CnDateofDate(DateGL){ 45 var CnData=new Array( 46 0x16,0x2a,0xda,0x00,0x83,0x49,0xb6,0x05,0x0e,0x64,0xbb,0x00,0x19,0xb2,0x5b,0x00, 47 0x87,0x6a,0x57,0x04,0x12,0x75,0x2b,0x00,0x1d,0xb6,0x95,0x00,0x8a,0xad,0x55,0x02, 48 0x15,0x55,0xaa,0x00,0x82,0x55,0x6c,0x07,0x0d,0xc9,0x76,0x00,0x17,0x64,0xb7,0x00, 49 0x86,0xe4,0xae,0x05,0x11,0xea,0x56,0x00,0x1b,0x6d,0x2a,0x00,0x88,0x5a,0xaa,0x04, 50 0x14,0xad,0x55,0x00,0x81,0xaa,0xd5,0x09,0x0b,0x52,0xea,0x00,0x16,0xa9,0x6d,0x00, 51 0x84,0xa9,0x5d,0x06,0x0f,0xd4,0xae,0x00,0x1a,0xea,0x4d,0x00,0x87,0xba,0x55,0x04 52 ); 53 var CnMonth=new Array(); 54 var CnMonthDays=new Array(); 55 var CnBeginDay; 56 var LeapMonth; 57 var Bytes=new Array(); 58 var I; 59 var CnMonthData; 60 var DaysCount; 61 var CnDaysCount; 62 var ResultMonth; 63 var ResultDay; 64 var yyyy=DateGL.getFullYear(); 65 var mm=DateGL.getMonth()+1; 66 var dd=DateGL.getDate(); 67 if(yyyy<100) yyyy+=1900; 68 if ((yyyy < 1997) || (yyyy > 2020)){ 69 return 0; 70 } 71 Bytes[0] = CnData[(yyyy - 1997) * 4]; 72 Bytes[1] = CnData[(yyyy - 1997) * 4 + 1]; 73 Bytes[2] = CnData[(yyyy - 1997) * 4 + 2]; 74 Bytes[3] = CnData[(yyyy - 1997) * 4 + 3]; 75 if ((Bytes[0] & 0x80) != 0) {CnMonth[0] = 12;} 76 else {CnMonth[0] = 11;} 77 CnBeginDay = (Bytes[0] & 0x7f); 78 CnMonthData = Bytes[1]; 79 CnMonthData = CnMonthData << 8; 80 CnMonthData = CnMonthData | Bytes[2]; 81 LeapMonth = Bytes[3]; 82 for (I=15;I>=0;I--){ 83 CnMonthDays[15 - I] = 29; 84 if (((1 << I) & CnMonthData) != 0 ){ 85 CnMonthDays[15 - I]++;} 86 if (CnMonth[15 - I] == LeapMonth ){ 87 CnMonth[15 - I + 1] = - LeapMonth;} 88 else{ 89 if (CnMonth[15 - I] < 0 ){CnMonth[15 - I + 1] = - CnMonth[15 - I] + 1;} 90 else {CnMonth[15 - I + 1] = CnMonth[15 - I] + 1;} 91 if (CnMonth[15 - I + 1] > 12 ){ CnMonth[15 - I + 1] = 1;} 92 } 93 } 94 DaysCount = DaysNumberofDate(DateGL) - 1; 95 if (DaysCount <= (CnMonthDays[0] - CnBeginDay)){ 96 if ((yyyy > 1901) && (CnDateofDate(new Date((yyyy - 1)+"/12/31")) < 0)){ 97 ResultMonth = - CnMonth[0];} 98 else {ResultMonth = CnMonth[0];} 99 ResultDay = CnBeginDay + DaysCount; 100 } 101 else{ 102 CnDaysCount = CnMonthDays[0] - CnBeginDay; 103 I = 1; 104 while ((CnDaysCount < DaysCount) && (CnDaysCount + CnMonthDays[I] < DaysCount)){ 105 CnDaysCount+= CnMonthDays[I]; 106 I++; 107 } 108 ResultMonth = CnMonth[I]; 109 ResultDay = DaysCount - CnDaysCount; 110 } 111 if (ResultMonth > 0){ 112 return ResultMonth * 100 + ResultDay;} 113 else{return ResultMonth * 100 - ResultDay;} 114 } 115 function CnYearofDate(DateGL){ 116 var YYYY=DateGL.getFullYear(); 117 var MM=DateGL.getMonth()+1; 118 var CnMM=parseInt(Math.abs(CnDateofDate(DateGL))/100); 119 if(YYYY<100) YYYY+=1900; 120 if(CnMM>MM) YYYY--; 121 YYYY-=1864; 122 return CnEra(YYYY)+"年"; 123 } 124 function CnMonthofDate(DateGL){ 125 var CnMonthStr=new Array("零","正","二","三","四","五","六","七","八","九","十","冬","腊"); 126 var Month; 127 Month = parseInt(CnDateofDate(DateGL)/100); 128 if (Month < 0){return "闰" + CnMonthStr[-Month] + "月";} 129 else{return CnMonthStr[Month] + "月";} 130 } 131 function CnDayofDate(DateGL){ 132 var CnDayStr=new Array("零", 133 "初一", "初二", "初三", "初四", "初五", 134 "初六", "初七", "初八", "初九", "初十", 135 "十一", "十二", "十三", "十四", "十五", 136 "十六", "十七", "十八", "十九", "二十", 137 "廿一", "廿二", "廿三", "廿四", "廿五", 138 "廿六", "廿七", "廿八", "廿九", "三十"); 139 var Day; 140 Day = (Math.abs(CnDateofDate(DateGL)))%100; 141 return CnDayStr[Day]; 142 } 143 function DaysNumberofMonth(DateGL){ 144 var MM1=DateGL.getFullYear(); 145 MM1<100 ? MM1+=1900:MM1; 146 var MM2=MM1; 147 MM1+="/"+(DateGL.getMonth()+1); 148 MM2+="/"+(DateGL.getMonth()+2); 149 MM1+="/1"; 150 MM2+="/1"; 151 return parseInt((Date.parse(MM2)-Date.parse(MM1))/86400000); 152 } 153 function CnEra(YYYY){ 154 var Tiangan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); 155 //var Dizhi=new Array("子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)","巳(蛇)", 156 //"午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"); 157 var Dizhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"); 158 return Tiangan[YYYY%10]+Dizhi[YYYY%12]; 159 } 160 function CnDateofDateStr(DateGL){ 161 if(CnMonthofDate(DateGL)=="零月") return " 请调整您的计算机日期!"; 162 else return "农历"+CnYearofDate(DateGL)+ " " + CnMonthofDate(DateGL) + CnDayofDate(DateGL); 163 } 164 function SolarTerm(DateGL){ 165 var SolarTermStr=new Array( 166 "小寒","大寒","立春","雨水","惊蛰","春分", 167 "清明","谷雨","立夏","小满","芒种","夏至", 168 "小暑","大暑","立秋","处暑","白露","秋分", 169 "寒露","霜降","立冬","小雪","大雪","冬至"); 170 var DifferenceInMonth=new Array( 171 1272060,1275495,1281180,1289445,1299225,1310355, 172 1321560,1333035,1342770,1350855,1356420,1359045, 173 1358580,1355055,1348695,1340040,1329630,1318455, 174 1306935,1297380,1286865,1277730,1274550,1271556); 175 var DifferenceInYear=31556926; 176 var BeginTime=new Date(1901/1/1); 177 BeginTime.setTime(947120460000); 178 for(;DateGL.getFullYear()<BeginTime.getFullYear();){ 179 BeginTime.setTime(BeginTime.getTime()-DifferenceInYear*1000); 180 } 181 for(;DateGL.getFullYear()>BeginTime.getFullYear();){ 182 BeginTime.setTime(BeginTime.getTime()+DifferenceInYear*1000); 183 } 184 for(var M=0;DateGL.getMonth()>BeginTime.getMonth();M++){ 185 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 186 } 187 if(DateGL.getDate()>BeginTime.getDate()){ 188 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 189 M++; 190 } 191 if(DateGL.getDate()>BeginTime.getDate()){ 192 BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); 193 M==23?M=0:M++; 194 } 195 var JQ="二十四节气"; 196 if(DateGL.getDate()==BeginTime.getDate()){ 197 JQ+=" 今日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 198 } 199 else if(DateGL.getDate()==BeginTime.getDate()-1){ 200 JQ+=" 明日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 201 } 202 else if(DateGL.getDate()==BeginTime.getDate()-2){ 203 JQ+=" 后日 <font color='#598F03'><b>"+SolarTermStr[M] + "</b></font>"; 204 } 205 else{ 206 JQ=" 二十四节气"; 207 if(DateGL.getMonth()==BeginTime.getMonth()){ 208 JQ+=" 本月"; 209 } 210 else{ 211 JQ+=" 下月"; 212 } 213 JQ+=BeginTime.getDate()+"日"+"<font color='#598F03'><b>"+SolarTermStr[M]+"</b></font>"; 214 } 215 return JQ; 216 } 217 </script>
引用:
<div class="time"><img src="http://img2.800fc.com/2013-03-13/nopic.jpg" name="timelogo" />
<script language="javascript">RunGLNL();</script>
</div>
加上某地区的天气:
<iframe vspace="0" height="20" marginheight="0" src="http://m.weather.com.cn/n/pn1/weather.htm?id=101230608T"
frameborder="0" width="235" marginwidth="0" scrolling="no" hspace="0"></iframe>
此处的ID是某地区的编号值,具体查中国天气网!