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

11,962

Solution 1

You can also use calendar function:

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

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

Solution 2

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'
Share:
11,962
Drakken Saer
Author by

Drakken Saer

I'm Drakken! I enjoy working with Ruby on Rails, AngularJS, PHP, HTML, CSS, jQuery, JavaScript, and other web technologies. I'm a bit of a programming hobbyist as I find it therapeutic and enjoy doing it on my free time. In my personal life, I'm a metal head, alternative model, all around nerd, vegan, and a humanitarian!

Updated on July 09, 2022

Comments

  • Drakken Saer
    Drakken Saer almost 2 years

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

  • Drakken Saer
    Drakken Saer over 8 years
    Thank you! I had read that in the docs originally but now that I re-read it I may have misunderstood it previously.
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    That should be moment().add(1, 'day').calendar(). (Note the incorrect output in your answer.)