ericalli
ericalli

Reputation: 1233

Get next week's date of a certain day in JavaScript

Depending on today's date (new Date()), I would like to get the date of the next Thursday at 7pm in javascript. For example:

If today's date is "Mon Apr 24 2017 13:00:00 GMT" I am looking for the result:

Thu Apr 27 2017 19:00:00 GMT

However, if today's date is "Thu Apr 27 2017 21:00:00 GMT" (a Thursday, but past 7pm) I am looking for the result:

Thu May 4 2017 19:00:00 GMT

Any help would be much appreciated!

Upvotes: 12

Views: 6974

Answers (3)

teroi
teroi

Reputation: 1087

Okay. So if moment.js is not available, work with the milliseconds.

var d = new Date();
var n = d.getTime(); // n == time in milliseconds since 1st Jan 1970
var weekday = d.GetDay() // 0...6 ; 0 == Sunday, 6 = Saturday, 4 = Thursday
var numDaysToNextThursday = weekday >= 4 ? 7 - (weekday-4) : 4 - weekday;
var nextThursday_msecs = n + numDaysToNextThursday * 24 * 60 * 60 * 1000;
var theDate = new Date(nextThursday_msecs); // this is the date

Edit: I can see that 7PM on Thursday is the magic moment. The above is not quite right in that regard; it will set theDate to be 7 days ahead if we are currently at Thursday regardless of actual time. You will probably still get the idea anyway.

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150020

Maybe something like the following (you can extend it if you want a more specific time than just hour and minute):

// day: 0=Sunday, 1=Monday...4=Thursday...
function nextDayAndTime(dayOfWeek, hour, minute) {
  var now = new Date()
  var result = new Date(
                 now.getFullYear(),
                 now.getMonth(),
                 now.getDate() + (7 + dayOfWeek - now.getDay()) % 7,
                 hour,
                 minute)

  if (result < now)
    result.setDate(result.getDate() + 7)
  
  return result
}

console.log(nextDayAndTime(4, 19, 0).toString()) // Thursday 7pm
console.log(nextDayAndTime(0, 19, 0).toString()) // Sunday 7pm
console.log(nextDayAndTime(1, 19, 0).toString()) // Monday 7pm (later today as of now in my timezone)
console.log(nextDayAndTime(1, 7, 30).toString()) // Monday 7:30am (next week, in my timezone)
console.log(nextDayAndTime(2, 19, 0).toString()) // Tuesday 7pm

The two key bits are now.getDate() + (7 + dayOfWeek - now.getDay()) % 7, which figures out the next Thursday (or specified day) starting from today (that's what the % 7 does, and then the if (result < now) that double-checks if the required time has passed yet today and if so adds a week.

Upvotes: 8

Kasia
Kasia

Reputation: 1725

You can easly get next thursday by simple recounting.

7 - number of days in a week 4 - index of thursday in a week calculation - you need to subtract index of current day of a week to get thursday

var day = new Date();

var days = 7 - day.getDay() + 4;

var nextDay = new Date(day.setDate(day.getDate() + days)); 


console.log(nextDay.toString());

codepen to check other days https://codepen.io/kejt/pen/LyRGBX?editors=1111

Upvotes: 3

Related Questions