开发者

Get date of specific day of the week in JavaScript

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.

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

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...

P.S. jQuery is cool too.


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


Use the DateJS library.

Date.parse('next wednesday');


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/

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.


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

The js (no jQuery needed) is:

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.


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

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


I recommend to use moment.js if working with dates.

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/


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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜