Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to call moment.calendar method without the time with JavaScript.

In this article, we’ll look at how to call moment.calendar method without the time with JavaScript.

Call the moment.calendar Method Without the Time with JavaScript

To call moment.calendar method without the time with JavaScript, 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'.

Conclusion

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

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 *