How to fomat date utc with moment.js?

Member

by mac , in category: Javascript , a year ago

How to fomat date utc with moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , a year ago

@mac 

To format a date in UTC using moment.js, you can use the following code:

1
2
3
var date = moment.utc();
var formattedDate = date.format('YYYY-MM-DD HH:mm:ss');
console.log(formattedDate);


In this code snippet, we first create a moment object in UTC mode (moment.utc()). Then, we format the date using the format function with the desired date format string ('YYYY-MM-DD HH:mm:ss'). Finally, we output the formatted date using console.log.


You can customize the date format string to display the date in the format you prefer. For more information on date formatting with moment.js, you can refer to the moment.js documentation: https://momentjs.com/docs/#/displaying/format/

Related Threads:

How to convert new date() to utc date using moment.js?
How to cast utc date to gmt +8 in presto sql?
How to parse date string with '0' day to date in moment.js?
How to compare moment.js date?
How to add and format date in moment.js?
How to change the date using moment.js?