Sometimes, we want to call moment.calendar method without the time in JavaScript.
In this article, we’ll look at how to call moment.calendar method without the time in JavaScript.
Call the moment.calendar Method Without the Time in JavaScript
To call moment.calendar method without the time in 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 in JavaScript, we can pass in null as the first argument.