如何处理固定日期的突变?

How to deal with mutations of a fixed date?

moment.js

.add()方法

Mutates the original moment by adding time.

var now = moment([2015, 11, 29, 14]);
var tomorrow = now.add(1, 'd');
// now has changed

如果我有一个固定的 'now' 时刻,我想稍后在我的程序中重复使用,那么正确的方法是什么?

我找到的最好的是这样的结构

var ref = [2015, 11, 29, 14];
var now = moment(ref);
var tomorrow = moment(ref).add(1, 'd');
var start = moment(ref).startOf('day'); // beginning of today
var end = moment(ref).add(1, 'd').endOf('day'); // end of tomorrow

但我觉得它很笨拙。

From their docs:

Note: It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.

If you want to create a copy and manipulate it, you should use moment#clone before manipulating the moment. More info on cloning.

var now = moment([2015, 11, 29, 14]);
var tomorrow = now.clone().add(1, 'd');
var start = now.clone().startOf('day'); // beginning of today
var end = now.clone().add(1, 'd').endOf('day'); // end of tomorrow

A quick look at their code on Github 表明他们所做的只是 return new Moment(this),因此如果更清楚的话,您可以通过 new Moment(now) 而不是 now.clone() 来做同样的事情你。我个人认为你应该使用任何你认为最清楚的方法。