Javascript 获取下周一的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33078406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:36:20  来源:igfitidea点击:

Getting the date of next Monday

javascriptdate

提问by nubteens

How can I get the next Monday in JavaScript? I can't find anything of this in the internet and I have also tried a lot of codes and understanding of this but I can't really do it.

如何在 JavaScript 中获得下周一?我在互联网上找不到任何这样的东西,我也尝试了很多代码和理解,但我真的做不到。

Here's my code:

这是我的代码:

var d = new Date();
var day = d.getDay();
d = new Date(d.setDate(d.getDate() + day + (day == 0 ? -6 : 2)));

回答by Beroza Paul

This will do:

这将:

var d = new Date();
d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
console.log(d);

回答by Jaromanda X

This will give next Monday if today is Monday

如果今天是星期一,这将在下星期一给出

var d = new Date();
d.setDate(d.getDate() + (7-d.getDay())%7+1);

This will result in today if today is Monday

如果今天是星期一,这将导致今天

var d = new Date();
d.setDate(d.getDate() + ((7-d.getDay())%7+1) % 7);

回答by trincot

If the actual day of the week you want is variable (Sunday, Thursday, ...) and you want a choice whether today could be a possible match or not, and it might be that you want to start with another date (instead of today), then this function may be useful:

如果您想要的实际星期几是可变的(星期日、星期四……),并且您想选择今天是否可能匹配,并且您可能想从另一个日期开始(而不是今天),那么这个功能可能会有用:

function getNextDayOfTheWeek(dayName, excludeToday = true, refDate = new Date()) {
    const dayOfWeek = ["sun","mon","tue","wed","thu","fri","sat"]
                      .indexOf(dayName.slice(0,3).toLowerCase());
    if (dayOfWeek < 0) return;
    refDate.setHours(0,0,0,0);
    refDate.setDate(refDate.getDate() + !!excludeToday + 
                    (dayOfWeek + 7 - refDate.getDay() - !!excludeToday) % 7);
    return refDate;
}

console.log("Next is: " + getNextDayOfTheWeek("Wednesday", false));

回答by Peter Jacoby

If you need to handle time zones, one option would be to use the UTCDate methods setUTCDate()and getUTCDate(). This allows for consistency so the calculation is the same no matter the time zone setting.

如果您需要处理时区,一种选择是使用 UTCDate 方法setUTCDate()getUTCDate()。这允许一致性,因此无论时区设置如何,计算都是相同的。

var d = new Date();
d.setUTCDate(d.getUTCDate() + (7 - d.getUTCDay()) % 7 + 1);

(This is the example from above where on a Monday the following Monday is returned)

(这是上面的示例,返回下一个星期一的星期一)

回答by Nazariy

var closestMonday = () => {
    var curr_date = new Date(); // current date
    var day_info = 8.64e+7; // milliseconds per day
    var days_to_monday = 8 - curr_date.getDay(); // days left to closest Monday
    var monday_in_sec = curr_date.getTime() + days_to_monday * day_info; // aleary Monday in seconds from 1970 
    var next_monday = new Date(monday_in_sec); // Monday in date object
    next_monday.setHours(0,0,0);
    return next_monday;
}

回答by riyadhalnur

Using moment.js,

使用moment.js

moment().add(7, 'days').calendar();

回答by Amit Shah

please try this,

请试试这个,

d = new Date(d);
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1);
alert(new Date(d.setDate(diff)));

Thanks Amit

谢谢阿米特