在 JavaScript 中获取一周中特定日期的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3638906/
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 05:32:32  来源:igfitidea点击:

Get date of specific day of the week in JavaScript

javascriptjquerydatetime

提问by Oscar Godson

I think i'm mainly having a brain fart. I just want to fast forward a Date()until a specific day of the week and then get the Month, Day, Year for that.

我想我主要是在放屁。我只想快进Date()直到一周中的特定一天,然后获得月、日、年。

E.g. today is 09/03/10 but i want a function (nextSession()) to return 09/08/10 (next wednesday).

例如,今天是 09/03/10,但我想要一个函数 ( nextSession()) 返回 09/08/10(下周三)。

How would I do this? The best thing I can think of is add a day to setDate()until getDay()== 3, but it's sorta ugly...

我该怎么做?我能想到的最好的事情是在== 3setDate()之前添加一天getDay(),但这有点难看......

P.S. jQuery is cool too.

PS jQuery 也很酷。

回答by pepkin88

function nextSession(date) {
    var ret = new Date(date||new Date());
    ret.setDate(ret.getDate() + (3 - 1 - ret.getDay() + 7) % 7 + 1);
    return ret;
}

This one will set date to next Wednesday. If it is already Wednesday, result will be one week later from given date.

这将设定日期为下周三。如果已经是星期三,则结果将在指定日期后一周。

EDIT: Added possibility of calling function without a parameter: nextSession()- it returns next session date from today

编辑:增加了在没有参数的情况下调用函数的可能性:nextSession()- 它从今天开始返回下一个会话日期

回答by Adam

Use the DateJS library.

使用DateJS 库

Date.parse('next wednesday');

回答by user113716

If you just need wednesday, you should be able to do something like this:

如果你只需要星期三,你应该能够做这样的事情:

function next(today) {
    var offset = (today.getDay() < 3) ? 0 : 7;
    var daysUntilWednesday = 3 + offset - today.getDay();
    return new Date().setDate(today.getDate() + daysUntilWednesday);
}

var wed = next( new Date() );


EDIT:

编辑:

or something like this? http://jsfiddle.net/zy7F8/1/

或类似的东西?http://jsfiddle.net/zy7F8/1/

var days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

function next(day) {

    var today = new Date();
    var today_day = today.getDay();

    day = day.toLowerCase();

    for (var i = 7; i--;) {
        if (day === days[i]) {
            day = (i <= today_day) ? (i + 7) : i;
            break;
        }
    }

    var daysUntilNext = day - today_day;

    return new Date().setDate(today.getDate() + daysUntilNext);

}

// insert a week day
alert(new Date(next( "Wednesday" )));?????????

EDIT:Made a correction for days entered that are yet to come in the same week.

编辑:对同一周内尚未到来的输入天数进行更正。

回答by Ender

Here you go: http://jsfiddle.net/ePQuv/

给你:http: //jsfiddle.net/ePQuv/

The js (no jQuery needed) is:

js(不需要jQuery)是:

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function subtractDays(myDate,days) {
    return new Date(myDate.getTime() - days*24*60*60*1000);
}

function dateOfNext(weekdayNumber) {
    var today = new Date();

    var lastSunday = subtractDays(today, today.getDay());

    var daysToAdd = weekdayNumber;
    if (weekdayNumber <= today.getDay()) {
        daysToAdd = daysToAdd + 7;
    }

    return addDays(lastSunday, daysToAdd);
}

What this does is subtract the current day of the week from the current date, to get to last sunday. Then, if today is after or equal to the "next day" you're looking for it adds 7 days + the day of the week that you want to get to, otherwise it just adds the day of the week. This will land you on the next of any given weekday.

这样做是从当前日期中减去一周中的当前日期,以获得最后一个星期日。然后,如果今天在或等于您要查找的“第二天”之后,它会添加 7 天 + 您想要到达的一周中的哪一天,否则它只会添加一周中的某一天。这将使您在任何给定工作日的下一个。

回答by Mic

Here is a function to return the next day number.

这是一个返回第二天数字的函数。

function nextDay(dayNb){
 return function( date ){
   return new Date(date.getTime() + ((dayNb-date.getDay() +7) %7 +1) *86400000);
 };
}

To use it, set the day number you want 0: Mon - 6: Sun

要使用它,请设置您想要的天数 0: Mon - 6: Sun

  var nextWed = nextDay(2);
  alert( nextWed( new Date() ));

回答by Kai Noack

I recommend to use moment.jsif working with dates.

如果处理日期,我建议使用moment.js

var nextmonday = moment().day(8).format('YYYY-MM-DD'); // e.g. 2015-10-26
var nexttuesday = moment().day(9).format('YYYY-MM-DD'); // e.g. 2015-10-27
var nextwednesday = moment().day(10).format('YYYY-MM-DD'); // e.g. 2015-10-28
var nextthursday = moment().day(11).format('YYYY-MM-DD'); // e.g. 2015-10-29

You can pick different formats and do even more nice stuff, check out: http://momentjs.com/

你可以选择不同的格式,做更多更好的东西,看看:http: //momentjs.com/

回答by ya_dimon

simple calculation:

简单计算:

//for next sunday, set 0, for saturday 6; wednesday == 3;
var needDay = 3;
var fromDate = new Date(); //or any date you calculate from
fromDate.setDate(fromDate.getDate() + (7 - fromDate.getDay()) + needDay);

console.log(fromDate);

+ (7 - fromDate.getDay()): adds to Date amount of days to change the date to sunday.
+ needDay: adds your day you need, if wednesday, just +3;

+ (7 - fromDate.getDay()):添加到 Date 天数以将日期更改为星期日。
+ needDay:添加您需要的日期,如果是星期三,只需 +3;