<script>
fun(2008, 30)//2008年的第30周
function fun(year, week) {
var d = new Date(year, 0, 1)
var time = d.getTime()
var t = week * 7 * 24 * 3600 * 1000
time += t
d = new Date(time)
var w = d.getDay()
var d1 = new Date(time - (w - 1) * 24 * 3600 * 1000)
var d2 = new Date(time + ((7 - w) * 24 * 3600 * 1000))
print(d1)
print(d2)
}
function print(d) {
var y = d.getFullYear()
var m = d.getMonth() + 1
m = m <10 ? "0" + m : "" + m
var day = d.getDate()
day = day <10 ? "0" + day : "" + day
var weekDay = d.getDay()
var wk = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
alert(y + "-" + m + "-" + day + " " + wk[weekDay])
}
</script>
//起始日期也在计算范围内function getRestDays(bd,ed)
{
var d1=new Date(bd),d2=new Date(ed)
var dateSpan=d2-d1
var days=parseInt(dateSpan/(24*3600*1000))+1//计算两个日期间的天数差,加1是为了把起始日期计算在内
var weeks=parseInt(days/7,10)
var result=weeks*2
if(days%7>0)
{
var leftdays=days%7
var week1=d1.getDay() //周日=0,周一=1,依次。。
if(week1==0)//如果第一个日期从周日开始,剩余天数不足一周(7天)
{
result +=1
}
else if(week1+leftdays>7)//如果第一个日期从周一到周六,加上剩余天数大于7,表示包含周六和周日,所以有两天
{
result +=2
}
else if(week1+leftdays==7)//如果刚好到周六,有一天休息日
{
result +=1
}
}
return result
}
执行结果可以自测,调用如下: