![]() |
VOOZH | about |
When working with dates and times in JavaScript, especially in applications that deal with users across different time zones, it is often necessary to work with dates in UTC (Coordinated Universal Time).
Below are the approaches to get a date in UTC using Moment.js:
Table of Content
This approach is used to get the current date and time in UTC. The moment().utc() method returns a Moment object representing the current date and time in UTC.
Example: In this example we are using the moment().utc() method to get the current date and time in UTC.
Output:
2024-08-28T11:09:30ZWhen we need to create a date in UTC we can use the moment.utc() method and pass the specific date and time as arguments. This method is useful when working with historical dates or scheduled events.
Example: In below example we are using creating the specific date in UTC.
Output:
2024-08-28T12:34:56ZWhen we have a local date and time that we need to convert to UTC we can first create a Moment object in local time and then use the .utc() method to convert it to UTC.
Example: In below example we are converting the the local time to the UTC.
Output:
2024-08-28T12:34:56Z