Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone moment js object

I have momentjs object in state

  state = {
     startDate: getNow() //this funtion return a momemtJS object
    }

in one function, I need to get the date one year before startDate

const dateBeforeOneYear = this.state.startDate.subtract(1, 'years');

But if I do like this, I modify the state by mistake

So I try to copy the state

const copyStartDate = {...this.state.startDate}

const copyStartDate = this.state.startDate.subtract(1, 'years');

But now I get the error, substract is not a function, I guess because copyStartDate is no more MomemntJs

like image 876
coinhndp Avatar asked Sep 14 '18 12:09

coinhndp


People also ask

How do you copy a moment object?

All moments are mutable. If you want a clone of a moment, you can do so implicitly or explicitly. Calling moment() on a moment will clone it. Additionally, you can call moment#clone to clone a moment.

What is clone () in moment?

Moment Clone will create a copy of the moment created.

Is MomentJS deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.

What is Moment () in JavaScript?

Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.

What is moment JS?

Moment.js provides a wrapper for the native JavaScript date object. In doing this, Moment.js extends the functionality and also accounts for several deficiencies in the object. Parsing is notably unpredictable with native date. For instance, suppose I am using a computer in the United States, but I have a date in DD/MM/YYYY format.

Why do I need to clone the moment before performing date math?

This means that operations like add, subtract, or set change the original moment object. As you can see, adding one week mutated a. To avoid situations like that, clone the moment before performing date math: There is a logical difference between time math and date math.

How do you clone a moment in Swift?

If you want a clone of a moment, you can do so implicitly or explicitly. Calling moment () on a moment will clone it. var a = moment (); var b = moment (a); a.year (2000); b.year (); // 2012 Additionally, you can call moment#clone to clone a moment.

Is it possible to clone an object in JavaScript?

However, for non-primitive data types (arrays, functions, and objects), which are passed by reference, we can always mutate the data, causing a single object value to have different content at different times. Cloning a JavaScript object is a task that is used mostly because we do not want to create the same object if it already exists.


2 Answers

There's a method to clone a moment object:

const yearBefore = this.state.startDate.clone().subtract(1, 'years');

It would also be a better idea to store a serialisable representation of the date in your component state, such as the result of calling .valueOf() on either a Date or a Moment, either of which returns the number of milliseconds since the UNIX epoch.

like image 170
Tom Fenech Avatar answered Oct 02 '22 19:10

Tom Fenech


moment.js has its own api for cloning moment object.

var copy = momentObj.clone();

And I agree on storing dates serialize representation instead of Object in store.

like image 36
bathpp Avatar answered Oct 02 '22 19:10

bathpp