我正在尝试制作一个倒计时时钟,直到2017年1月20日就职典礼大约中午12:00使用flipclock.js
如何计算当前时间与该日期/时间之间的差异(以秒为单位)?
我目前有:
var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = inauguration - now;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
搞定了:
var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
var now = Date.now();
var diff = inauguration.getTime() - now;
diff = diff / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
答案 0 :(得分:3)
目前inauguration
是Date
个对象。您可以通过.getTime()
函数获取秒数。你需要改变:
var diff = inauguration.getTime() - now;
//---------------------^^^^^^^^^^
您的工作代码应为:
var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
var now = Date.now();
var diff = inauguration.getTime() - now;
diff = diff / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
getTime()
的想法是解决方案的一部分,以及其他一些小的改动,上面的代码现在正在运作。
此外,原始实现也有效:
var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = (inauguration.getTime() - now) / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.min.js"></script>
<div id="clock"></div>