如何使用 moments.js 从分钟整数中获取格式为 "hh:mm" 的持续时间?
How to get a duration in format-"hh:mm" from minutes-integer with moments.js?
我有一个整数值,表示分钟数。我正在尝试使用 moments.js 以小时和分钟显示分钟,即使分钟超过一天:
- 500 会产生“08:20”
- 1600 将产生“26:40”
这是我目前所拥有的:
function getDuration(value){
return moment.utc().startOf('day').add(value, 'minutes').format('hh:mm')
}
以上代码有效,唯一的问题是当分钟值超过 20 小时时,它会再次从零开始。
提前致谢!
如果您可以满足于简单的 javascript 实现,这里有一些适合您的东西:
function getDuration(n) {
var hours = Math.floor(n / 60);
var minutes = n % 60;
return pad(hours) + ':' + pad(minutes);
}
function pad(s) {
s = s + '';
return s.length < 2 ? ('00' + s).substr(s.length, 2) : s;
}
document.write('see: ' + getDuration(1600));
document.write(', see: ' + getDuration(500));
document.write(', see: ' + getDuration(481));
document.write(', see: ' + getDuration(11600));
我有一个整数值,表示分钟数。我正在尝试使用 moments.js 以小时和分钟显示分钟,即使分钟超过一天:
- 500 会产生“08:20”
- 1600 将产生“26:40”
这是我目前所拥有的:
function getDuration(value){
return moment.utc().startOf('day').add(value, 'minutes').format('hh:mm')
}
以上代码有效,唯一的问题是当分钟值超过 20 小时时,它会再次从零开始。
提前致谢!
如果您可以满足于简单的 javascript 实现,这里有一些适合您的东西:
function getDuration(n) {
var hours = Math.floor(n / 60);
var minutes = n % 60;
return pad(hours) + ':' + pad(minutes);
}
function pad(s) {
s = s + '';
return s.length < 2 ? ('00' + s).substr(s.length, 2) : s;
}
document.write('see: ' + getDuration(1600));
document.write(', see: ' + getDuration(500));
document.write(', see: ' + getDuration(481));
document.write(', see: ' + getDuration(11600));