admin管理员组文章数量:1323335
I have a petition
-module and when I post the petition, there is a deadline for when the petition ends. My API JSON returns an enddate. I want to use the MomentJS
plugin and so far i have simply added:
<script src="/js/moment.min.js"></script>
and my html:
<time class="newstime" datetime="2014-08-04T10:00:00.000Z">//DISPLAY REMAINING TIME HERE</time>
How do I achieve that it displays the remaining time?
thanks in advance
I have a petition
-module and when I post the petition, there is a deadline for when the petition ends. My API JSON returns an enddate. I want to use the MomentJS
plugin and so far i have simply added:
<script src="/js/moment.min.js"></script>
and my html:
<time class="newstime" datetime="2014-08-04T10:00:00.000Z">//DISPLAY REMAINING TIME HERE</time>
How do I achieve that it displays the remaining time?
thanks in advance
Share Improve this question edited Jul 21, 2017 at 8:39 Rachel Gallen 28.6k22 gold badges75 silver badges86 bronze badges asked Jun 24, 2014 at 8:28 SHTSHT 7204 gold badges19 silver badges39 bronze badges 2- what's in your script? – Rachel Gallen Commented Jun 24, 2014 at 8:30
- helpful link: stackoverflow./questions/16129157/… – Kamlesh Meghwal Commented Jun 24, 2014 at 8:35
2 Answers
Reset to default 7There is a plugin for this called Moment-countdown It can be localixzed using bitbucket
here is a piece of code from 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
you can do your own by creating a function that refreshes itself every second:
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>
where in our case d2 is an arbitrary future date. Don't forget months in js are counted from 0, not one. so 7 = August (not July)
var d2 = new Date(2016, 7, 3, 18, 0,0,0);
hope it's clear.
本文标签: javascriptMomentJSHow to show remaining timeStack Overflow
版权声明:本文标题:javascript - MomentJS - How to show remaining time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135821a2422363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论