How to display the time of day with moment.js?

Member

by aubrey , in category: Javascript , 2 months ago

How to display the time of day with moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 2 months ago

@aubrey 

You can display the time of day using moment.js by creating a moment object with the current date and time, and then formatting it to display only the time part. Here's an example:

1
2
3
4
5
6
7
8
// Create a moment object with the current date and time
let now = moment();

// Format the moment object to display only the time
let timeOfDay = now.format('hh:mm A');

// Display the time of day
console.log(timeOfDay);


This will output the current time in the format "hh:mm AM/PM", for example "09:30 AM". You can customize the format string according to your preferences using the formatting tokens provided by moment.js.