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
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.
Moment Clone will create a copy of the moment created.
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With