Open In App

Moment.js Parsing Moment Clone

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to clone and parse moment.js objects.

Cloning and Parsing Moment.js Objects: All Moment.js objects are parsable and clonable. Cloning a moment could be done, both implicitly and explicitly. 

Installation:

npm install moment

 

Syntax:

moment(Moment); // Moment is a moment.js object

Parameters: It accepts a moment as a parameter

Return value: It returns a clone of a moment passed as an argument.

Example 1: In this example, we will implicitly clone a moment. 

main.js:

Javascript




const moment = require('moment');
var a = moment([2022]);
var b = moment(a);
a.year(2000);
console.log(b.year());


Step to run the application: Open the terminal and type the following command. 

node main.js

Output:

2022

Example 2: In this example, we will explicitly clone a moment. 

main.js:

Javascript




const moment = require('moment');
var a = moment([2022]);
var b = a.clone();
a.year(2000);
b.year();


Step to run the application:

node main.js

Output:

2022

Reference: https://momentjs.com/docs/#/parsing/moment-clone/


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads