Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js: Get day relevant to today (i.e. "Tomorrow, today, yesterday, etc")

How can I get Moment.js to return "Today" or other relevant terms? I cannot find anything in the docs that cover this.

like image 869
Drakken Saer Avatar asked Nov 29 '15 20:11

Drakken Saer


People also ask

How can I get tomorrow date in moment?

let today = moment(); let tomorrow = moment(). add(1,'days'); let yesterday = moment(). add(-1, 'days');

How can I get yesterdays date from moment?

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. Save this answer.

How do I get weekday from date in moment?

The moment(). weekday() method is used to get or set the weekday of the Moment object. It is locale aware hence the value can depend based on whether the first day of the week is a Sunday or Monday.


2 Answers

I hope this will help you.

moment('2013-02-04T10:35:24-08:00').fromNow(); // '3 years ago'
moment().subtract('days', 0).fromNow(); // 'a few seconds ago'
moment().subtract('days', 1).fromNow(); // 'a day ago'
moment().subtract('days', 7).fromNow(); // '7 days ago'
like image 172
Meeh Avatar answered Sep 30 '22 18:09

Meeh


You can also use calendar function:

moment().calendar(moment().add(1, 'day')); // "Yesterday at 9:14 PM"

http://momentjs.com/docs/#/displaying/calendar-time/

like image 30
xersiee Avatar answered Sep 30 '22 18:09

xersiee