How to get yesterday's date with Momentjs?

JavascriptDateMomentjs

Javascript Problem Overview


So, my question is simple, how do I get yesterday's date with MomentJs ? In Javascript it is very simple, i.e.

today = new Date();
yesterday = new Date(today.setDate(today.getDate() - 1))

console.log(yesterday)

But how do I achieve this with MomentJs ?

Javascript Solutions


Solution 1 - Javascript

Just like this: moment().subtract(1, 'days'). It will give you the previous day with the same exact current time that is on your local pc.

Solution 2 - Javascript

Also :

moment().subtract(1, 'day')

It will give you the previous day with the same exact current time that is on your local pc.

Solution 3 - Javascript

When we get yesterday's date, there are three possibilties

1. Get yesterday date with current timing

moment().subtract(1, 'days').toString()

2. Get yesterday date with start of the day

moment().subtract(1, 'days').startOf('day').toString()      

3. Get yesterday date with end of the day

moment().subtract(1, 'days').endOf('day').toString()

Solution 4 - Javascript

moment().add(-1, 'days');

You can find more information in the docs.

Solution 5 - Javascript

You can easily subtract days from moment using

var yesterday = moment().subtract(1, 'days')

And for finding the previous date

var previousDay = moment('2017/11/6', 'YYYY/MM/DD').subtract(1, 'days')

Solution 6 - Javascript

Simplest Solution

By using moment we can easily get Yesterday, Today and Tomorrow Date

1. Get Yesterday Date:

moment().subtract(1, "days").format("YYYY-MM-DD");

2. Get Today Date:

moment().subtract(0, "days").format("YYYY-MM-DD");

3. Get Tomorrow Date:

moment().subtract(-1, "days").format("YYYY-MM-DD");

Solution 7 - Javascript

Yesterday's date in Momentjs in DD-MM-YYYY format.

const yesterdaydate = moment().subtract(1, "days").format("DD-MM-YYYY");
console.log(yesterdaydate)

Solution 8 - Javascript

This worked for me:

var yesterday = new Date(dateInput.getTime());
yesterday.setDate(yesterday.getDate() - 1);
console.log(yesterday);

var tomorrow = new Date(dateInput.getTime());
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(tomorrow);

dateB = moment(yesterday).format("YYYYMMDD");
dateA = moment(tomorrow).format("YYYYMMDD");
console.log(dateB);
console.log(dateA);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionToTaView Question on Stackoverflow
Solution 1 - JavascriptAleksView Answer on Stackoverflow
Solution 2 - JavascriptSai RamView Answer on Stackoverflow
Solution 3 - JavascriptFizer KhanView Answer on Stackoverflow
Solution 4 - JavascriptJan HommesView Answer on Stackoverflow
Solution 5 - JavascriptShanavas VHView Answer on Stackoverflow
Solution 6 - JavascriptFarrukh MalikView Answer on Stackoverflow
Solution 7 - JavascriptJojo JosephView Answer on Stackoverflow
Solution 8 - JavascriptLouise Fitzpatrick HayesView Answer on Stackoverflow