admin管理员组文章数量:1278981
I am trying to convert the number 20 into minutes with moment js. I need
21 to to look like 21:00
This way for my set interval the timer will show 19:58 to 0:01.
I had tried
var x = 1260000;
var d = moment.duration(x, 'milliseconds');
var hours = Math.floor(d.asHours());
var mins = Math.floor(d.asMinutes()) - hours * 60;
This still leaves me with 21. I am not sure how to get the minutes form.
This seems like it shoud be a short fix. Any help is greatly appreciated.
I am trying to convert the number 20 into minutes with moment js. I need
21 to to look like 21:00
This way for my set interval the timer will show 19:58 to 0:01.
I had tried
var x = 1260000;
var d = moment.duration(x, 'milliseconds');
var hours = Math.floor(d.asHours());
var mins = Math.floor(d.asMinutes()) - hours * 60;
This still leaves me with 21. I am not sure how to get the minutes form.
This seems like it shoud be a short fix. Any help is greatly appreciated.
Share Improve this question edited Aug 29, 2016 at 9:50 Winnemucca asked Aug 29, 2016 at 9:38 WinnemuccaWinnemucca 3,45811 gold badges39 silver badges66 bronze badges 3- You can do this without moment.js using the standard constructor: var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); – Rob Commented Aug 29, 2016 at 9:41
- Could you give us what input you've got, and what output you're trying to get? It's quite hard to pick apart your code without the context. – Whothehellisthat Commented Aug 29, 2016 at 9:42
- Sorry if that isn't clear. I am trying get get the milliseconds to look like 21:00. I need a countdown to go to 0:01. However, i just keep getting 21 – Winnemucca Commented Aug 29, 2016 at 9:52
4 Answers
Reset to default 4var number = 15;
moment(number.toString(),"LT")
output 15:00
other method var value = 6.8 (exemple : 34/5 (34 hours of work / 5 days)) /** * Convertion d'un nombre en Moment * @param value */ convertNumberToMoment(value: number): moment.Moment {
var hours = Math.floor(value);
var minutes = Math.round((value % 1)*100)/100;
return moment(hours + ':' + minutes * 60 + '0',"LT");
}
output 6:48
How to create a new moment object from hours-string and to display it as HH:MM :
var input = '21';
moment(input,'HH').format('HH:mm'); // 21:00
You can use moment-duration-format plug-in and duration subtract
method.
The plug-in lets you format duration object and with the subtract
method you can modify duration value.
Here a working example that may help you:
var $container = $('#timer');
var x = 1260000;
var d = moment.duration(x, 'milliseconds');
var intervalId = setInterval(function(){
d.subtract(1, 's');
if( d.seconds() >= 0 ){
var timer = d.format('m:ss', { trim: false });
$container.html(timer);
} else {
clearInterval(intervalId);
}
}, 1000);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare./ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
<div id="timer"></div>
var moment = require("moment")
var x = 1260000;
var d = moment.duration(x, 'milliseconds');
moment(d.asMinutes(),'mm').format('mm:ss');
本文标签: momentjsHow to convert number to time format in javascriptStack Overflow
版权声明:本文标题:momentjs - How to convert number to time format in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238200a2363446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论