Skip to content
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

Calling add() on a moment object causes it to be mutated #4474

Closed
felamaslen opened this issue Mar 4, 2018 · 2 comments
Closed

Calling add() on a moment object causes it to be mutated #4474

felamaslen opened this issue Mar 4, 2018 · 2 comments

Comments

@felamaslen
Copy link

Description of the Issue and Steps to Reproduce:

When calling .add on a moment object, the object is mutated in a way which is hard to debug. For example:

const { expect } = require('chai');
const moment = require('moment');

const date1 = moment(new Date('2018-03-04'));

expect(date1.format('YYYY-MM-DD')).to.equal('2018-03-04'); // succeeds

const date2 = date1.add(3, 'days'); // this *mutates* date1 so that date1 is now 2018-03-07 (!)

expect(date2.format('YYYY-MM-DD')).to.equal('2018-03-07'); // succeeds
expect(date1.format('YYYY-MM-DD')).to.equal('2018-03-04'); // FAIL: date1 has been mutated

Environment:

Node.JS v8.9.1 on Ubuntu 17.10 (this really doesn't matter)

Output of the following:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)
Sun Mar 04 2018 18:59:10 GMT+0000 (GMT)
2018-3-4 18:59:10
0
undefined
2.21.0

Ensure your issue is isolated to moment. Issues involving third party tools will be closed unless submitted by the tool's author/maintainer.

@felamaslen
Copy link
Author

The same behaviour is also exhibited when running .startOf() instead of .add().

@marwahaha
Copy link
Member

We're aware of this. Personally, I don't love the mutability, but we haven't released 3.0 (it's been a few years - see more info here).

One of the moment contributors just released Luxon which has an immutable API. If you (or any folks reading) want to help us release an immutable version of Moment, let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants