168

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 ?

ToTa
  • 3,304
  • 2
  • 19
  • 39

8 Answers8

280

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.

Tundra Fizz
  • 473
  • 3
  • 10
  • 25
Aleks
  • 3,906
  • 3
  • 24
  • 29
  • 6
    Tried this at face value but didn't work for me. I had to provide some type of format to the result. E.g. 'moment().subtract(1, 'days').calendar()' or 'moment().subtract(1, 'days').toDate()' or 'moment().subtract(1, 'days').format("MM/DD/YYYY")' – Andrew May 18 '20 at 19:21
108

Also :

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

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

Sai Ram
  • 4,196
  • 4
  • 26
  • 39
  • I'm using this method since a month in a snippet, and I just found out that this is not work properly at the beginning of the month. So `moment(date).subtract(-1, 'day')` on the first day gives back the last day of the current month instead of the last day of previous month. Does anybody know more about? – Rico Jul 01 '19 at 15:31
  • can you try with `moment().subtract(1, 'day')` instead of `moment().subtract(-1, 'day')` – Sai Ram Jul 01 '19 at 17:35
  • @sam forget the minus, was just a typo. Is there cause I tryed `moment(date).add(-1, 'day')` with the same result. – Rico Jul 02 '19 at 06:32
66

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()
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
10
moment().add(-1, 'days');

You can find more information in the docs.

Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
9

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");
Farrukh Malik
  • 746
  • 9
  • 13
8

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')

Shanavas VH
  • 153
  • 2
  • 11
4

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

const yesterdaydate = moment().subtract(1, "days").format("DD-MM-YYYY");
console.log(yesterdaydate)
Jojo Joseph
  • 1,493
  • 1
  • 15
  • 12
0

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);