【问题标题】:Javascript: Returning how many seconds to an date in a format 0000-00-00 00:00:00Javascript:以 0000-00-00 00:00:00 格式返回日期的秒数
【发布时间】:2014-06-24 13:01:46
【问题描述】:

我正在使用 FlipClock 对某个日期进行倒计时。我倒计时的日期格式如下:0000-00-00 00:00:00。

我想知道距离日期还有多少秒。所以我可以在我的翻转时钟中使用。

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

【问题讨论】:

标签: javascript date


【解决方案1】:

我猜您的日期是 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;
}

【讨论】:

  • 谢谢!效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2017-09-21
  • 1970-01-01
  • 2013-06-09
  • 2015-07-19
  • 2013-07-12
  • 2016-06-04
相关资源
最近更新 更多