WebDevTmas / moment-round

Plugin for momentjs to round time in dates.
Other
47 stars 75 forks source link

Why the rounding functions have to modify the moment object #10

Open xyz1hang opened 7 years ago

xyz1hang commented 7 years ago

Just curious about the reason why the moment object gets modified after calling floor, ceil or round. What if you want to get both floor and ceil of same moment. Do you have to make a deep copy of the moment object ?

Please correct me if I missed something

console.log();
var m1 = moment();
console.log(m1);
m1.floor(10, 'minutes');
console.log(m1);

console.log();
var m2 = moment();
console.log(m2);
m2.ceil(10, 'minutes');
console.log(m2);
murraybauer commented 7 years ago

clone the moment object first before rounding. m1.clone().floor(..) Moment always mutates the object - e.g. for .add() or .subtrack()