瞬间js添加功能不起作用

时间:2018-07-17 10:43:09

标签: javascript fullcalendar momentjs

从字面上看,moment()。add()在我的js代码中不起作用。

var theDate = moment(event.start.format("YYYY-MM-DD HH:mm")); //start Date of event 
var checkquarter = theDate.add(30, 'minutes');
var plus = 30;
if (userDuration == '45') {
  plus = 45;
}
for (var i = 0; i < excludedList.length; i++) {

  var excludedTomorrow = moment(excludedList[i]["excludedDate"]).format("YYYY-MM-DD HH:mm"); //start Date of event that should be excluded

  var endtime = moment(excludedTomorrow).add(plus, 'minutes'); //endTime of event that should be excluded
  if (excludedList[i]["id"] == 'aaa@gmail.com') {
    console.log(endtime);
    console.log(plus);
    console.log(theDate);
    console.log(checkquarter);

  }
  //var endtimeForCompare = moment(endtime);
  if (theDate >= excludedTomorrow && theDate < endtime && Id == excludedList[i]["id"]) {

    return false;
  }
}
<script src='https://momentjs.com/downloads/moment.js'></script>

在这段代码中,我使用矩加法两次。首先是checkquarter,将其添加到Date变量中30分钟。第二个是结束时间,它是我从数据库中获得的,加上被排除在明天之外的分钟数。

这是控制台 console response 如果您看到此图像,它将按顺序显示结束时间,日期,checkquarter变量。但是,如您所见,没有添加任何内容。

我的momentJS脚本是

<script src='https://momentjs.com/downloads/moment.js'></script>

1 个答案:

答案 0 :(得分:0)

您应该将theDate克隆到checkQuarter中,因为时刻是可变的。

https://momentjs.com/docs/#/manipulating/

这意味着var checkquarter = theDate.add(30, 'minutes');正在更改theDate,而checkQuarter只是对theDate的另一个引用。

运行以下命令时,请查看控制台:

var theDate = moment("1995-12-25 14:00");
console.log(theDate.toString());
var newDate = theDate.add(10, "minutes");
console.log(theDate.toString());
console.log(newDate.toString());
var anotherDate = moment(theDate);
anotherDate.add(10, "minutes");
console.log(anotherDate.toString());
console.log(theDate.toString());