'Moment.js internal object what is "_d" vs "_i"
I am using Moment.js and manipulating a date using moment.hour(xx) moment.minute(xx).
When i console.log the moment i see that the object contains a _d and _i:
the _d contains the correct changed moment.hour() or moment.minute() changes however the _i object contains the original?
k {_isAMomentObject: true, _i: Thu Dec 11 2014 20:34:00 GMT+0200 (South Africa Standard Time), _isUTC: false, _pf: Object, _locale: j…}
_d: Thu Dec 11 2014 14:00:00 GMT+0200
_i: Thu Dec 11 2014 20:34:00 GMT+0200
Could anyone enlighten me?
Solution 1:[1]
Pay no attention to those. Use the various output functions, such as .format() instead. See the Moment.js guidance on this topic. In short, all fields that are prefixed with an underscore (_) should be considered off limits.
The moment internals have some quirks due to how the Date object works. All of the functions in the public API take them into account, but you probably don't want to figure them out yourself.
Just to be complete though, I'll elaborate on their purpose:
_iis the input used when create themomentobject. It can be a string, a number, an array, or aDateobject.However, if another
momentobject is passed in, the_iwill be copied to that moments_i, and other properties will also be copied over._iwill never be amomentobject._ican also be undefined, in the case of creating the current moment withmoment()._dis the instance of theDateobject that backs themomentobject.If you are in "local mode", then
_dwill have the same local date and time as the moment object exhibits with the public API. The timestamps returned bygetTimeorvalueOfwill also match.If you are in "UTC mode", then
_dwill still have the same UTC date and time as the moment object exhibits with the public API. This may be confusing, as you'd need to look atgetUTCDateand other UTC-based functions on_din order to see them match. The timestamps will still match here as well.If you've changed the time zone offset, with the
utcOffset,zone, ortzfunctions, then the_dvalue cannot stand alone. It must also consider if_offsetis defined. If it is, then the timestamp backing the_dobject has to first be adjusted by the amount of the offset. You can see this behavior in the implementation of thevalueOfmethod here.Also, if you look at the string output of
_dwhen a different offset or time zone has been applied, it will appear that_dis using the local time zone. However, that conversion to local time is simply a side effect of thetoStringfunction of theDateobject. Moment does not use that result in its functions.
This is the behavior for these two fields as of the current version (2.10.6 as I'm writing this). However, there are other fields as well, and since these are internal fields, it's entirely possible the behavior could change in a future version. In particular, see issue #2616.
Solution 2:[2]
As a complement to @Matt's answer:
Checkout this result from the chrome's console:
date1 is a moment's valid object:

As you can see, ._d and ._i have different values. So you better use the format() function (as @Matt Johnson wrote) inside your source code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | RolandoCC |
