我需要你的帮助。
下面的当前代码能够计算日期,从给定日期算起的一周(7 天)。
我希望对其进行修改,以便使用今天的日期并计算下周的日期(星期一和星期五)。
即。今天的日期是:2014 年 10 月 12 日(月/日/年)
下周的星期一是:12/15/2014
下周的星期五是:12/19/2014
function nextweek() {
var currentDate = new Date();
var w = new Date(currentDate.getTime() + 7 * 24 * 60 * 60 * 1000);
var dd = w.getDate()
var mm = w.getMonth() + 1
var yyyy = w.getFullYear()
return mm + '/' + dd + '/' + yyyy
}
最佳答案
这里有一些代码片段,适用于我前段时间看到的类似问题(不记得确切的来源,它是关于获取本周的星期一)
Date.prototype.getNextWeekMonday = function() {
var d = new Date(this.getTime());
var diff = d.getDate() - d.getDay() + 1;
if (d.getDay() == 0)
diff -= 7;
diff += 7; // ugly hack to get next monday instead of current one
return new Date(d.setDate(diff));
};
Date.prototype.getNextWeekFriday = function() {
var d = this.getNextWeekMonday();
return new Date(d.setDate(d.getDate() + 4));
};
// You can then use it like this :
var date = new Date();
alert(date.getNextWeekMonday());
alert(date.getNextWeekFriday());
应该在敏感的日子(周日和周一)进行测试,以确保它返回正确的一周的周一和周五。
关于javascript - 获取下周的星期一和星期五,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27403585/