有谁知道如何根据本周日期获得下周的约会?例如,如果我有这个星期四的日期(25/6/2009),我如何使用javascript来获取下一个星期四的日期(2009年2月7日)?
答案 0 :(得分:51)
var firstDay = new Date("2009/06/25");
var nextWeek = new Date(firstDay.getTime() + 7 * 24 * 60 * 60 * 1000);
如果您喜欢“流利的”API,也可以查看DateJS。
答案 1 :(得分:34)
function nextweek(){
var today = new Date();
var nextweek = new Date(today.getFullYear(), today.getMonth(), today.getDate()+7);
return nextweek;
}
答案 2 :(得分:10)
function dateObject.getNextWeekDay返回对象自己的日期之后的下一个工作日。
Date.prototype.getNextWeekDay = function(d) {
if (d) {
var next = this;
next.setDate(this.getDate() - this.getDay() + 7 + d);
return next;
}
}
var now = new Date();
var nextMonday = now.getNextWeekDay(1); // 0 = Sunday, 1 = Monday, ...
var secondNextMonday = new Date(nextMonday).getNextWeekDay(1);
console.log('Next Monday : ' + nextMonday);
console.log('Second Next Monday : ' + secondNextMonday);
答案 3 :(得分:9)
Date.prototype.addDays = function (d) {
if (d) {
var t = this.getTime();
t = t + (d * 86400000);
this.setTime(t);
}
};
this_week.addDays(7);
答案 4 :(得分:1)
下周返回的函数:
function getNextWeekDay (startDate, dayOfWeek){
var dayOffset = dayOfWeek > startDate.getDay()
? dayOfWeek - startDate.getDay()
: dayOfWeek - startDate.getDay() + 7;
startDate.setDate(startDate.getDate() + dayOffset);
return startDate;
}
var now = new Date();
var nextMonday = getNextWeekDay(new Date(),1); // 0 = Sunday, 1 = Monday, ...
var nextSaturday = getNextWeekDay(new Date(),6);
var nextSunday = getNextWeekDay(new Date(),0);
var secondNextMonday = getNextWeekDay(new Date(now.getTime() + ( 7 *24 * 60 * 60 * 1000)),1);
alert(nextMonday+ '- '+nextSaturday+ '- '+nextSunday+ '- '+secondNextMonday);
答案 5 :(得分:1)
const getDateAfterWeek = (week) => {
let today = new Date();
const nextweek = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate() + 7 * week,
);
return nextweek;
}
答案 6 :(得分:0)
一个更直接的解决方案是
let nextWeek = new Date()
nextWeek.setDate(nextWeek.getDate() + 7)
请注意,它会保留当天的当前时间。
答案 7 :(得分:0)
这是我的解决方案。
var today = new Date; // get current date
var first = today.getDate() - today.getDay() + 1 + 7; // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(today.setDate(first));
var lastday = new Date(today.setDate(last));
console.log(firstday, lastday);