繁体   English   中英

MomentJS-如何显示剩余时间

[英]MomentJS - How to show remaining time

我有一个competition模块,当我发布比赛时,有一个比赛结束的截止日期。 我的API JSON返回结束日期。 我想使用MomentJS插件,到目前为止,我仅添加了:

<script src="/js/moment.min.js"></script>

和我的HTML:

<time class="newstime" datetime="2014-08-04T10:00:00.000Z">//DISPLAY REMAINING TIME HERE</time>

如何实现显示剩余时间?

提前致谢

有一个名为Moment-countdown的插件,可以使用bitbucket进行本地化

这是git hub的一段代码

//from then until now
moment("1982-5-25").countdown().toString(); //=> '30 years, 10 months, 14 days, 1 hour, 8 minutes, and 14 seconds'

//accepts a moment, JS Date, or anything parsable by the Date constructor
moment("1955-8-21").countdown("1982-5-25").toString(); //=> '26 years, 9 months, and 4 days'

//also works with the args flipped, like diff()
moment("1982-5-25").countdown("1955-8-21").toString(); //=> '26 years, 9 months, and 4 days'

//accepts all of countdown's options
moment().countdown("1982-5-25", countdown.MONTHS|countdown.WEEKS, NaN, 2).toString(); //=> '370 months

您可以创建一个每秒刷新一次的函数来自己动手:

 var t=setInterval(runFunction,1000); function runFunction(){ var d = new Date(); var d2 = new Date(2016, 7, 3, 18, 0,0,0); var milSec = d2-d; var d3 = new Date(milSec); nrDays = (Math.floor(d3/1000/60/60/24)); nrHours = (Math.floor(d3/1000/60/60))%24; nrMin = (Math.floor(d3/1000/60))%60; nrSec = (Math.floor(d3/1000))%60; document.getElementById("countdown").innerHTML = nrDays +" days: " + nrHours+" hours: "+ nrMin + " min " + nrSec +" sec"; } 
 <!DOCTYPE html> <html> <body> <p id="countdown">here</p> </body> </html> 

在我们的例子中d2是任意的将来日期。 不要忘记js中的月份从0开始计数,而不是从1开始计数。 所以7 = 8月(不是7月)

var d2 = new Date(2016, 7, 3, 18, 0,0,0);

希望很清楚。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM