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

Member

by aubrey , in category: Javascript , a year ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , a year 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.

Related Threads:

How to get start of day and end of day in moment.js?
How to get values for time intervals of hours in one day in sparql?
How to add an odd day to a time in php?
How to properly get day of the week (name) with moment.js?
How to validate a string has a proper day month and year with moment.js?
How to parse date string with '0' day to date in moment.js?