当下js添加功能不起作用 [英] moment js add function is not working

查看:79
本文介绍了当下js添加功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var theDate = moment(event.start.format("YYYY-MM-DD HH:mm"));//事件的开始日期var checkquarter = theDate.add(30,'minutes');var plus = 30;如果(userDuration =='45'){加= 45;}for(var i = 0; i< excludeedList.length; i ++){var excludeToTorowrow = moment(excludedList [i] ["excludedDate"]).format("YYYY-MM-DD HH:mm");//应该排除的事件的开始日期var endtime = moment(excludedTomorrow).add(plus,'minutes');//应排除的事件的结束时间如果(excludedList [i] ["id"] =='aaa@gmail.com'){console.log(endtime);console.log(plus);console.log(theDate);console.log(checkquarter);}//var endtimeForCompare = moment(endtime);if(theDate> = excludeedTomorrow& theDate< endtime&&Id == excludeedList [i] ["id"]){返回false;}}  

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

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

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

我的momentJS脚本是

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

解决方案

您应该将 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,分钟");console.log(anotherDate.toString());console.log(theDate.toString()); 

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>

In this code, I used moment add twice. First is checkquarter, which is added 30 minutes to theDate variable. Second is endtime, which is added plus minutes to excludedTomorrow, which I get from my DB.

Here is the console console response if you see this image, it shows the endtime, plus, theDate, checkquarter variable in order. However, as you see, nothing is added to them.

My script for momentJS is,

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

解决方案

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

这篇关于当下js添加功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆