JS计算本周一和本周五的日期
代码不长:
var today=new Date();
var weekday=today.getDay();
var monday=new Date(1000*60*60*24*(1-weekday) + today.getTime());
var friday=new Date(1000*60*60*24*(5-weekday) + today.getTime());
目前monday和friday都是Date类型的,要得到字符串类型,可以考虑下面的函数:
function getDateStr(dd){
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
m=parseInt(m,10);
if(m<10){
m="0"+m;
}
var d = dd.getDate();
d=parseInt(d,10);
if(d<10){
d="0"+d;
}
return y+"-"+m+"-"+d;
}
用jQuery使起来就是这样的:
$("#dateFromSpan").html(getDateStr(monday));
$("#dateToSpan").html(getDateStr(friday));