![]() |
VOOZH | about |
The moment().utc() method is used to specify that the given Moment object's timezone would be displayed as UTC. An optional parameter can be passed that preserves the current time value and only changes the timezone to UTC.
Syntax:
moment().utc( Boolean );Parameters:
Return Value: This method returns the Moment object with the new timezone.
Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.
Step 1: Run the following commands to initialize the project and create an index file & env file. (Make sure you have node and npm installed)
npm init -yStep 2: Installing required packages
npm install momentProject Structure:
👁 ImageThe updated dependencies in package.json file will look like:
"dependencies": {
"moment": "^2.30.1",
}
Example 1: This example shows the use of moment and printing the time in the console.
Output:
MomentOne is: Tue Nov 21 2023 14:07:18 GMT+0000
MomentOne hours: 14
MomentOne minutes: 7
MomentOne is: Tue Nov 21 2023 14:07:18 GMT+0000
MomentOne hours in UTC: 14
MomentOne minutes in UTC: 7
Example 2: This example shows the use of moment and printing the time in the console.
Output:
MomentTwo is: Tue Nov 21 2023 14:07:49 GMT+0000
MomentTwo hours: 14
MomentTwo minutes: 7
MomentTwo is: Tue Nov 21 2023 14:07:49 GMT+0000
MomentTwo hours in UTC: 14
MomentTwo minutes in UTC: 7