VOOZH about

URL: https://www.geeksforgeeks.org/javascript/moment-js-moment-utcoffset-method/

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


  • Courses
  • Tutorials
  • Interview Prep

Moment.js moment().utcOffset() Method

Last Updated : 8 Aug, 2022

The moment().utcOffset() method is used to specify the given Moment object's UTC offset. An optional parameter can be passed that preserves the current time value and only changes the offset.

Syntax:

moment().utcOffset( Number | String, [Boolean] );

Parameters: This method accepts two parameters as mentioned above and described below:

  • Number | String: It is a number or string that specifies the offset in minutes or hours.
  • Boolean: It is an optional boolean value that specifies whether the offset would be changed without modifying the actual time itself.

Return Value: This method returns the Moment object with the new offset.

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.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment().utcOffset() Method.

Example 1:

Output:

Utc Offset of MomentOne: 60
MomentOne is: Moment<2022-08-05T14:56:08+01:00>
Utc Offset of momentTwo: 150
MomentTwo is: Moment<2022-08-05T16:26:08+02:30>
Utc Offset of momentThree: -510
MomentThree is: Moment<2022-08-05T05:26:08-08:30>

Example 2:


Output:

Utc Offset of moment1: 120
moment1 is: Moment<2022-08-05T15:56:08+02:00>
Utc Offset of moment2: 300
moment2 is: Moment<2022-08-05T18:56:08+05:00>
Utc Offset of moment3: -360
moment3 is: Moment<2022-08-05T07:56:08-06:00>

Reference: https://momentjs.com/docs/#/manipulating/utc-offset/

Comment