Moment.js is a JavaScript library that helps you do pretty much anything you could ever need with dates! Specifically, it helps you parse, validate, manipulate, and display dates in JavaScript.

 

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!

Install Moment.js

Moment can be installed using all of the popular package managers. I installed it with npm like this:

Days as numbers

The JavaScript Date object has a getDay() method which returns a number for which day of the week it currently is. Sunday is 0, Monday is 1 and so on. So we can do something like this: