52

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.

Here's my code:

var d = new Date();
var day = d.getDay();
d = new Date(d.setDate(d.getDate() + day + (day == 0 ? -6 : 2)));
Tushar
  • 85,780
  • 21
  • 159
  • 179
nubteens
  • 5,462
  • 4
  • 20
  • 31

11 Answers11

78

This will retrieve the next Monday, returning the current date if already a Monday:

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

To return the following Monday even if the current date is a Monday:

var d = new Date();
d.setDate(d.getDate() + (((1 + 7 - d.getDay()) % 7) || 7));
console.log(d);
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Beroza Paul
  • 2,047
  • 1
  • 16
  • 16
  • 7
    that's not working for all cases, for example : "09/26/2017" it gives 32 :) you should test the month and calcule days number in each month – Mourad Idrissi Sep 19 '17 at 10:31
  • 3
    @MouradIdrissi It gives the correct result for me. `var d = new Date("2017-09-26")`, followed by the code above yields Monday 2 Oct, 2017, which is the next Monday after Tuesday 26 Sep, 2017. – Victor Zamanian Dec 05 '17 at 12:30
  • 1
    @VictorZamanian is correct; if you pass 32 as the arg for setDate() into a month with 30 days, you will get the 2nd date of the following month. – Sublow Aug 06 '20 at 13:32
  • Only the date becomes correct here. The time (hours, minutes, seonds) changes. – Typewar Jan 09 '21 at 12:07
  • 2
    I don't think this works – if today is Monday then this will set the date to today. `d.getDay()` will be 1, `1 + 7 -1` will be 7, `7 % 7` will be 0, so you'll get `setDate(d.getDate() + 0)` – so, not the next Monday, but the same day. – Alex Apr 13 '21 at 02:23
32

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));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 4
    This is exactly what i needed and wonder why this answer isn't getting more upvote ... is there a way to format the output to DD MM YYYY? @trincot – Kolawole Emmanuel Izzy Jun 09 '19 at 23:48
  • @KolawoleEmmanuelIzzy, sure there is. There is `toLocaleDateString` which has many formatting options. Check out the many questions on the subject. – trincot Jun 10 '19 at 08:32
  • 1
    @trincot Your solution is very flexible and practical. Personally I would change the boolean addition/subtraction, and that's mostly because TypeScript won't allow it. I would use unary plus to explicitly convert the boolean to 0 or 1. – Robert Penner Sep 05 '19 at 19:22
  • Absolutely the best answer – mpyw Jun 22 '23 at 15:21
21

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);
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • this works fine in my console, but not in my nodejs code – Bubunyo Nyavor Apr 11 '17 at 00:04
  • 1
    works identically for me - if you gave a little more information than *it doesn't work* that'd be just peachy - considering this code gives the same results as the code you accepted, I think you are probably doing it wrong – Jaromanda X Apr 11 '17 at 01:46
  • I dropped this code in JS fiddle. It worked fine. When I dropped it into my meteor app yesterday, I was getting a date for 14 – Bubunyo Nyavor Apr 11 '17 at 06:00
5

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.

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)

Peter Jacoby
  • 2,406
  • 25
  • 26
5

This is my solution

var days =[1,7,6,5,4,3,2];
var d = new Date();
d.setDate(d.getDate()+days[d.getDay()]);
console.log(d);
Vu Phan
  • 553
  • 6
  • 6
3
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;
}
Nazariy
  • 408
  • 3
  • 10
3

Other responses seems not to take into account that "next Monday" should be at the beginning of the day. Here's how I've implemented it recently:

function getNextMonday() {
  const now = new Date()
  const today = new Date(now)
  today.setMilliseconds(0)
  today.setSeconds(0)
  today.setMinutes(0)
  today.setHours(0)

  const nextMonday = new Date(today)

  do {
    nextMonday.setDate(nextMonday.getDate() + 1) // Adding 1 day
  } while (nextMonday.getDay() !== 1)

  return nextMonday
}
Frenchcooc
  • 910
  • 6
  • 20
1

Or more simple:

let now = new Date();
let day = 1; // Monday

if (day > 6 || day < 0) 
  day = 0;
    
while (now.getDay() != day) {
  now.setDate(now.getDate() + 1);
}
now.setDate(now.getDate() + 1);

console.log(now); // next Monday occurrence
backciro
  • 11
  • 2
1

Another way is to find the offset (in number of days) to the day you want, turn that into milliseconds, and add it to you starting date:

let d = new Date();
const day = d.getDay();
const targetDay = 1;  // Monday
let dayOffset = targetDay - day;
if (dayOffset < 0) dayOffset += 7;

d = new Date(d.getTime() + (dayOffset * 24 * 3600 * 1000));
Will59
  • 1,430
  • 1
  • 16
  • 37
0

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)));
Dharman
  • 30,962
  • 25
  • 85
  • 135
Amit Shah
  • 1,380
  • 1
  • 10
  • 19
0

Using moment.js,

moment().add(7, 'days').calendar();
riyadhalnur
  • 394
  • 5
  • 14
  • 6
    The question was not > Which library / Framework can i use ? – Anonymous0day Oct 12 '15 at 12:21
  • 10
    Is it mentioned anywhere in the original question? The person was looking for a solution and I gave mine using a popular library. – riyadhalnur Oct 14 '15 at 12:56
  • 6
    But also, this simply adds 7 days to the current day, it doesn't get you the next Monday on any arbitrary day that the code is run on.. – Nick B May 11 '20 at 22:30