<script>
var currDT;
var aryDay = new Array("日","一","二","三","四","五","六");
//初始页面
function ini() {
currDT = new Date();
showDate();
}
//上一周 或 下一周
function addWeek(ope) {
var num = 0;
if(ope=="-") {
num = -7;
}
else if(ope=="+") {
num = 7;
}
currDT = addDate(currDT,num);
showDate();
}
function showDate() {
span1.innerHTML = currDT.toLocaleDateString(); //显示日期
var dw = currDT.getDay();
var tdDT;
//确定周一是那天
if(dw==0) {
tdDT = addDate(currDT,-6);
}
else {
tdDT = addDate(currDT,(1-dw));
}
//在表格中显示一周的日期
var objTB = document.getElementById("tb1");
for(var i=0;i<7;i++) {
if(tdDT.toLocaleDateString()==currDT.toLocaleDateString()) {
objTB.rows[0].cells[i].style.color = "red"; //currDT突出显示
}
dw = tdDT.getDay();
objTB.rows[0].cells[i].innerHTML = tdDT.getMonth()+1 + "月" + tdDT.getDate() + "日 星期" + aryDay[dw];
tdDT = addDate(tdDT,1); //下一天
}
}
//增加或减少若干天,由 num 的正负决定,正为加,负为减
function addDate(dt,num) {
var ope = "+";
if(num<0) {
ope = "-";
}
var reDT = dt;
for(var i=0;i<Math.abs(num);i++) {
reDT = addOneDay(reDT,ope);
}
return reDT;
}
//增加或减少一天,由ope决定, + 为加,- 为减,否则不动
function addOneDay(dt,ope) {
var num = 0;
if(ope=="-") {
num = -1;
}
else if(ope=="+") {
num = 1;
}
var y = dt.getYear();
var m = dt.getMonth();
var lastDay = getLastDay(y,m);
var d = dt.getDate();
d += num;
if(d<1) {
m--;
if(m<0) {
y--;
m = 11;
}
d = getLastDay(y,m);
}
else if(d>lastDay) {
m++;
if(m>11) {
y++;
m = 0;
}
d = 1;
}
var reDT = new Date();
reDT.setYear(y);
reDT.setMonth(m);
reDT.setDate(d);
return reDT;
}
//是否为闰年
function isLeapYear(y) {
var isLeap = false;
if(y%4==0 && y%100!=0 || y%400==0) {
isLeap = true;
}
return isLeap;
}
//每月最后一天
function getLastDay(y,m) {
var lastDay = 28;
m++;
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) {
lastDay = 31;
}
else if(m==4 || m==6 || m==9 || m==11) {
lastDay = 30;
}
else if(isLeapYear(y)==true) {
lastDay = 29;
}
return lastDay;
}
</script>
<body onload="ini()"> <!--加载时初始页面-->
<span style="cursor:hand;font-weight:bold;" onclick="addWeek('-')" title="上一周">←</span>
<span id="span1"></span>
<span style="cursor:hand;font-weight:bold;" onclick="addWeek('+')" title="下一周">→</span>
<table id="tb1" border="1" style="font-size:10pt">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>