- 首页 > it技术 > >
//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}
//
function getXDate(year, weeks, weekDay) {
weekDay %= 7;
var date = new Date(year, "0", "1");
var time = date.getTime();
weekDay == 0 ? time += weeks * 7 * 24 * 3600000 : time += (weeks - 1) * 7 * 24 * 3600000;
//这里需要注意,现在这种模式是以周日为一周的结束,如果设定周日为一周的开始,去掉这个判断,选择后者。
date.setTime(time);
return getNextDate(date, weekDay);
}
function getNextDate(nowDate, weekDay) {
var day = nowDate.getDay();
var time = nowDate.getTime();
var sub = weekDay - day;
time += sub * 24 * 3600000;
nowDate.setTime(time);
return nowDate;
}
//注意,其中的参数:第一个为年,第二个为第几周,第三个为周数,0为周日,1为周一;
alert("1983第1周的星期1是:" + formatDate(getXDate(1983, 1, 1)));
alert("1983第1周的星期日是:" + formatDate(getXDate(1983, 1, 0)));
推荐阅读