3 ms·
Disclaimer: I too use moment in my daily project Example snippet (CMIIW, it's as I remembered): let date1 = moment("2020-01-01"); let date2 = date1.ad
by fendy3002 6y ago
Disclaimer: I too use moment in my daily project
Example snippet (CMIIW, it's as I remembered):
let date1 = moment("2020-01-01");
let date2 = date1.add(2, "days");
console.log(date1.format("YYYY-MM-DD")); // 2020-01-03
date2.add(2, "days");
console.log(date1.format("YYYY-MM-DD")); // 2020-01-05
Usually the date1.add operation won't change the date1 value, and any operation to date2 won't change date1, but it isn't. It's made worse because people like to return moment object outside function scope, so operation outside can modify the object inside and weird bugs happen.
It's actually a manageable problem IF you know the behavior, and only using moment object reference inside scope, and using immutable objects as parameter / return, such as milliseconds timestamp and js date.