VOOZH about

URL: https://www.geeksforgeeks.org/node-js/moment-js-moment-utc-method/

⇱ Moment.js moment().utc() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Moment.js moment().utc() Method

Last Updated : 8 Jan, 2025

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:

  • Boolean: It is a boolean value that specifies whether the timezone would be changed without changing the actual time itself.

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.

Steps to Install the Node App:

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 -y

Step 2: Installing required packages

npm install moment

Project Structure:

👁 Image

The 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
Comment

Explore