Javascript:以格式0000-00-00 00:00:00返回到日期的秒数

时间:2014-06-24 13:01:46

标签: javascript date

我使用FlipClock对特定日期进行倒计时。我倒计时的日期是这样的格式:0000-00-00 00:00:00。

我希望得到这个日期之前的秒数。所以我可以在我的flipclock中使用。

var clock = $('#countdown').FlipClock(seconds, {
    clockFace: 'HourlyCounter',
    countdown: true,
    callbacks: {
      stop: function() {
          //callback
      }
    }
});

1 个答案:

答案 0 :(得分:0)

我猜您的日期符合ISO 8601格式的yyyy-mm-dd hh:mm:ss,应该被解释为当地时间。在这种情况下,解析字符串,将其转换为Date对象并从现在减去它以获得以毫秒为单位的差异。除以1,000以得到秒的差异,例如

// Parse string in yyyy-mm-dd hh:mm:ss format
// Assume is a valid date and time in the system timezone
function parseString(s) {
  var b = s.split(/\D+/);
  return new Date(b[0], --b[1], b[2], b[3], b[4], b[5]);
}

// Get the difference between now and a provided date string
// in a format accepted by parseString
// If d is an invalid string, return NaN
// If the provided date is in the future, the returned value is +ve
function getTimeUntil(s) {
  var d = parseString(s);
  return d? (d - Date.now())/1000|0 : NaN;
}