New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatting a moment object breaks equality comparisons #3080
Comments
The reason for this is that .format() checks the isValid property for the first time. That property is evaluated and set. You're basically seeing the same issue that is described in #2894. I'm curious though, why do you want to do this sort of comparison? There are tons of properties on a moment that really have nothing to do with the actual date value. For instance, because moment has an internal property _i that tracks the original input value, the following two moments won't be the same in that sort of equality comparison, even though they are the exact same point in time: moment('2016-01-01')
moment('01/01/2016', 'MM/DD/YYYY') If you want to know whether two moments are at the same point in time, .isSame() is the proper way to do it. |
@maggiepint the reason this is a problem is for tracking changes to a model. In my example I have a As you can see this is less of a comparison of two different objects, more of a comparison in time of the same object. |
I'll have to think about the best solution for your situation, but I think it's worth mentioning that moments have a lot of internal properties. Run moment() in the REPL and check it out. You maybe want to preserve either moment().valueOf() or just the formatted string on your model for comparison purposes. |
@maggiepint yea I understand there are lots of internal properties, and that the deep checking is probably not performant, but for the needs I have, performance is nearly irrelevant as it is a on-save event, so very infrequent and client-side. I think a better solution would be to store the internal moment properties as non-enumerable, as both lodash |
@maggiepint to resolve my immediate problem, I can just define in my angular filters/formatting to first run a |
I suspect you should usually call |
Should we drop support for IE8? This escalated quickly! |
I'm +1 on dropping IE8, but it should be on a major version change. |
I think this works now. Just tested on Chrome. Please re-open if it's still a problem. |
When a moment object is formatted, the equality of the object with a clone of the unformatted object becomes false. This is unexpected, as formatting operations should be nullipotent (not change the underlying object).
Small example:
See https://jsfiddle.net/x4hr9qmq/1/for fiddle
Note that
===
will always fail, but this is expected asmoment.clone()
should return a different object, but equality comparitors like lodash's_.isEqual
and angularangular.equals
should return true as they compare properties not object identity.The text was updated successfully, but these errors were encountered: