Javascript传递时间差值,以小时*分钟*秒*1000为单位

Javascript传递时间差值,以小时*分钟*秒*1000为单位,javascript,Javascript,我有这个JS脚本: if ($('.count-down').length !== 0){ $('.count-down').countdown({ timestamp : (new Date()).getTime() + 24*60*60*1000 }); } 它提供24*60*60*1000的+24小时,所以脚本从24:00:00开始倒计时 我需要将我的活动日期作为有价值的格式Y.m.d H:2014.12.31 12:00提供给该脚本,它将计算现在之间的时

我有这个JS脚本:

if ($('.count-down').length !== 0){
    $('.count-down').countdown({
        timestamp : (new Date()).getTime() + 24*60*60*1000
    });
}
它提供24*60*60*1000的+24小时,所以脚本从24:00:00开始倒计时

我需要将我的活动日期作为有价值的格式Y.m.d H:2014.12.31 12:00提供给该脚本,它将计算现在之间的时差并将其添加到代码中

var difference = getTimeDifferenceFromNow('2014.12.31 12:00')
timestamp : (new Date()).getTime() + difference

减去两个
Date
实例,将得到两者之间的长度,单位为毫秒

function getTimeDifferenceFromNow(timeString) {
  return new Date(timeString) - new Date();
}

getTimeDifferenceFromNow('2014.12.31 12:00') // 818501769, roughly 9 days and half in the future.
这是怎么回事?当你减去两个
Date
实例时,它们的
valueOf()
方法在内部被调用,这将把它们转换成时间戳。然后你实际上是在复制两个时间戳,最后得到一个毫秒数

function getTimeDifferenceFromNow(timeString) {
  return new Date(timeString) - new Date();
}

getTimeDifferenceFromNow('2014.12.31 12:00') // 818501769, roughly 9 days and half in the future.

编辑

然而,我想知道你为什么这样做?如果您想要给定日期/时间的时间戳,为什么不实例化它并获取它的时间戳呢

new Date('2014.12.31 12:00').getTime() // 1419998400000

这是最终的解决方案:

    var eventdate  = new Date("January 1, 2015 1:00:00").getTime();
if ($('.count-down').length !== 0){
    $('.count-down').countdown({
            timestamp :  eventdate
    });
}

那么,你的问题到底是什么?我正在尝试,但不起作用,代码中有什么错误:var eventdate=new Date('2014.12.31 12:00')。getTime();如果($('.count-down').length!==0){$('.count-down').countdown({timestamp:eventdate});}@RokasPaškevičius那么倒计时在做什么呢?你能提供一把小提琴吗?