Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Literally, moment().add() is not working in my js code.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
248 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should clone theDate into checkQuarter as Moments are mutable.

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

this means that var checkquarter = theDate.add(30, 'minutes'); is changing theDate and checkQuarter is just another reference to theDate.

Have a look at the console when you run the following :

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());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...