Categories
JavaScript Answers

How to Call the moment.calendar Method Without the Time?

Spread the love

To call moment.calendar method without the time, we can pass in null as the first argument.

For instance, we can write:

const cal = moment('2021-01-01')
  .calendar(null, {
    lastDay: '[Yesterday]',
    sameDay: '[Today]',
    nextDay: '[Tomorrow]',
    lastWeek: '[last] dddd',
    nextWeek: 'dddd',
    sameElse: 'L'
  })
console.log(cal)

We call moment with a date string.

Then we call calendar on the moment object with null and an object that has some date formatting options.

sameDay lets us specify the format for same day.

lastDay lets us specify the format for yesterday.

nextDay lets us specify the format for tomorrow.

lastWeek lets us specify the format for last week.

nextWeek lets us specify the text for next week.

sameElse specifies the format for all other dates.

Therefore, cal is '01/01/2021'.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *