Today I was writing a little program and I needed to find the date of the “next Monday”. If today is Monday, it should return today. If today is Tuesday – Sunday, it should return the following Monday.
Here’s how you can do that with Moment.js!
Moment can be installed using all of the popular package managers. I installed it with npm like this:
npm install moment --save
Days as numbers
// Require Moment.js
const moment = require('moment')
const dayNumber = 1 // 1 for Monday, 2 for Tuesday, use whatever day you need!
// Get a Date object for the next Monday
const nextDay = moment().day(dayNumber) // returns a moment date object
// Format it if you want!
console.log(nextDay.format() // something like 2016-04-05T21:59:19-07:00