dayjs subtract and add is mutating and changing the dayjs object

Created on 5 Nov 2020  路  4Comments  路  Source: iamkun/dayjs

Describe the bug
I am not sure if this has changed, at least I haven't read anything online about it, but it seems like some dayjs operations are not immutable. I experienced this for the add and subtract operations. According to the docs and issue #844 these operations/methods should not be mutable and the expected behavior should be like the expected behavior below.

const now = dayjs()
console.log('today', now.format())
const sevenYearsAgo = now.subtract(7, 'year');
console.log('7 years ago', sevenYearsAgo.format())
console.log('today', now.format())

Outputs

today 2020-11-05T16:30:03+00:00
7 years ago 2013-11-05T16:30:03+00:00
today 2013-11-05T16:30:03+00:00

You can test this out on in the console on the dayjs website
Expected behavior
It should be immutable and not change when calling either add or subtract

today 2020-11-05T16:30:03+00:00
7 years ago 2013-11-05T16:30:03+00:00
today 2020-11-05T16:30:03+00:00

Information

  • Day.js Version: 1.8.36
  • OS: Mac OS 10.15.7
  • Browser: Version 86.0.4240.183 (Official Build) (x86_64)
  • Time zone: GMT0
鈽笍Bug

All 4 comments

This is really weird.

Yes, Day.js should always be immutable.

I've tested on the node environment, and it works as expected. https://runkit.com/embed/7ytaf9qqtzr6

However, if tested on a browser script, it will fail.

dayjs extend badMutable plugin in website?
https://day.js.org/docs/en/plugin/bad-mutable#docsNav

@UlricGan Correct! I'll remove it soon. THX

This is a website build error, not Day.js itself. Fixed.

Was this page helpful?
0 / 5 - 0 ratings