Open In App

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

Last Updated : 28 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().duration().clone() Method is used to clone the given Duration object. As the Duration object is mutable, it can be used to save a copy of the current Duration like a snapshot by using this method, including all its properties and attributes.

Syntax:

moment().duration().clone();

Parameters: This method does not accept any parameters.

Return Value: This method returns the cloned Duration object.

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

Example 1:

Javascript




const moment = require('moment');
  
let durationOne = moment.duration(9, 'months');
console.log(
    "durationOne data:",
    durationOne.toISOString()
);
  
let durationTwo = durationOne.clone();
console.log(
    "durationTwo data:", durationTwo.toISOString()
);


Output:

durationOne data: P9M
durationTwo data: P9M

Example 2:

Javascript




const moment = require('moment');
  
let durationA = 
    moment.duration({months: 10, days: 7, hours: 3});
console.log(
    "durationA data:", durationA.toISOString()
);
  
let durationB = durationA.clone();
console.log(
    "durationB data:", durationB.toISOString()
);


Output:

durationA data: P10M7DT3H
durationB data: P10M7DT3H
durationC data: P10M7DT3H

Reference: https://momentjs.com/docs/#/durations/clone/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads