1

我有点困惑!我有以下 JS

//Set datetimes for the centre of the Timeline and the highlighting period
var myCurrentDateTime = new Date();
var MS_PER_MINUTE = 60000;
var MinsToDeductForCentre = 1440
var MinsEitherSideOfNow = 720
var myTLcenterdate = new Date(myCurrentDateTime - (MinsToDeductForCentre * MS_PER_MINUTE));
var tldate = myTLcenterdate.toString()
var hlStartDate = new Date(myCurrentDateTime - (MinsEitherSideOfNow * MS_PER_MINUTE));
var hlEndDate = new Date(myCurrentDateTime + (MinsEitherSideOfNow * MS_PER_MINUTE));    

myTLcentredate 给了我当前时间前 24 小时的日期

hlStartDate 给了我当前时间前 12 小时的日期

但是 hlEndDate 给出了 NotANumber 错误...

它基本上将 myCurrentDateTime 和 MinsEitherSideOfNow * MS_PER_MINUTE (43200000) 的值视为字符串并将它们连接起来,例如“Thu May 17 10:44:17 UTC+0100 201243200000”

为什么?

4

3 回答 3

4

如果您想从日期中添加或减去分钟,这就足够了:

var dSubtract = new Date, 
    dAdd = new Date;
// let dSubtract be Thu May 17 2012 12:11:54 GMT+0200
// and dAdd         Thu May 17 2012 12:11:54 GMT+0200
dSubtract.setMinutes(dSubtract.getMinutes()-1440); 
//=> dSubtract is now: Wed May 16 2012 12:00:54 GMT+0200
dAdd.setMinutes(dAdd.getMinutes()+720);
//=> dAdd is now: Fri May 18 2012 00:00:54 GMT+0200

(当然在我的时区中的例子)

于 2012-05-17T10:15:05.510 回答
0

在两个变量上使用函数 parseInt,如下所示:

parseInt(var1) * parseInt(var2)
于 2012-05-17T10:13:49.340 回答
0

试试这个

function SubtractTime(objStartdatetime, objEnddatetime) {
//var T1 = new Date("2012/02/14 8:10:00");

d = new Date();
     T1 = new Date(objStartdatetime);
     T2 = new Date(objEnddatetime);        
      misec = T2.getTime() - T1.getTime();

}

function AddTime(objStartdatetime, objEnddatetime) { //var T1 = new Date("2012/02/14 8:10:00");

d = new Date();
     T1 = new Date(objStartdatetime);
     T2 = new Date(objEnddatetime);        
      misec = T2.getTime() + T1.getTime();

}

于 2012-05-17T10:16:09.797 回答