VOOZH about

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

⇱ Moment.js moment.duration().toISOString() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Moment.js moment.duration().toISOString() Method

Last Updated : 15 Jun, 2022

The moment().duration().toISOString() Method is used to get an ISO8601 standard formatted string of the given duration. The UTC mode is used for the timestamp regardless of the locale of the moment, so that consistency can be maintained with the native Date API of JavaScript. This behaviour can be disabled by passing true to the keepOffset parameter.

Note: The library will try to use the native Date toISOString() method for better performance.

Syntax:

moment().duration().toISOString(keepOffset);

Parameters: This method accepts a single parameter as mentioned above and described below:

  • keepOffset: This parameter is used to specify whether UTC conversion is enabled or not. It is an optional parameter.

Return Value: This method returns the duration as the ISO String format.

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.duration().toISOString() Method.

Example 1:

Output:

ISOString of durationOne is: P9M
ISOString of durationTwo is: P1Y7M 
ISOString of durationThree is: P70D

Example 2:

Output:

ISOString of durationA is: P4M5DT9H23S
ISOString of durationB is: PT3H36M6S 
ISOString of durationC is: P2Y6M9D

Reference: https://momentjs.com/docs/#/displaying/as-iso-string/

Comment

Explore