我是否能够在持续时间对象上使用 moment.js 的 format
方法?我在文档中没有找到它的任何信息,并且似乎也不是持续时间对象的属性。
我想要做这样的事情:
var diff = moment(end).unix() - moment(start).unix();
moment.duration(diff).format('hh:mm:ss')
另外,如果有其他能够轻松实现此类功能的库,我会对推荐很感兴趣。
谢谢!
我是否能够在持续时间对象上使用 moment.js 的 format
方法?我在文档中没有找到它的任何信息,并且似乎也不是持续时间对象的属性。
我想要做这样的事情:
var diff = moment(end).unix() - moment(start).unix();
moment.duration(diff).format('hh:mm:ss')
另外,如果有其他能够轻松实现此类功能的库,我会对推荐很感兴趣。
谢谢!
// set up
let start = moment("2018-05-16 12:00:00"); // some random moment in time (in ms)
let end = moment("2018-05-16 12:22:00"); // some random moment after start (in ms)
let diff = end.diff(start);
// execution
let f = moment.utc(diff.asMilliseconds()).format("HH:mm:ss.SSS");
alert(f);
DDD HH:mm:ss.SSS
(其中DDD
表示一年中的第几天)会显示1
,而我想要的是0
。有什么快速解决方法吗? - Andrew Mao将持续时间转换为毫秒,然后转换为 moment:
moment.utc(duration.as('milliseconds')).format('HH:mm:ss')
我们正在研究在moment.js中添加某种格式化时间持续时间的方法。请参见https://github.com/timrwood/moment/issues/463
还有一些其他可能会有所帮助的库,如http://countdownjs.org/和https://github.com/icambron/twix.js
.format
完全相同?这太疯狂了。已经五年了,还没有完成?真的吗?! - kristopoloushh:mm:mm
并且只有10秒钟,它会显示 10
而不是 00:00:10
(即使使用了 forcelength)。如果这不是格式化问题...那么它应该被称为其他名称,格式应该标准化。 - Piotr Kula{ trim: false }
,它将停止这种行为。 - Carlos Martinezconst duration = moment.duration(value, 'seconds');
,那么可以使用以下格式进行格式化:moment.utc(duration.as('milliseconds')).format('HH:mm:ss').toString();
。 - Evgeny Bobkin使用以下代码行:
moment.utc(moment.duration(4500, "seconds").asMilliseconds()).format("HH:mm:ss")
var diff = moment(end).unix() - moment(start).unix();
moment.utc(moment.duration(diff).asMilliseconds()).format("HH:mm:ss.SSS");
对于我的特定用例来说,最好的情况是:
var duration = moment.duration("09:30"),
formatted = moment.utc(duration.asMilliseconds()).format("HH:mm");
这比 @Wilson 的回答更好,因为它不访问私有内部属性 _data。
你不需要使用.format方法。可以像这样使用持续时间:
const duration = moment.duration(83, 'seconds');
console.log(duration.minutes() + ':' +duration.seconds());
// output: 1:23
我在这里找到了解决方案:https://github.com/moment/moment/issues/463
编辑:
并且添加了对秒、分和小时的填充:
const withPadding = (duration) => {
if (duration.asDays() > 0) {
return 'at least one day';
} else {
return [
('0' + duration.hours()).slice(-2),
('0' + duration.minutes()).slice(-2),
('0' + duration.seconds()).slice(-2),
].join(':')
}
}
withPadding(moment.duration(83, 'seconds'))
// 00:01:23
withPadding(moment.duration(6048000, 'seconds'))
// at least one day
duration = moment.duration(7200, 'seconds'); withPadding(duration);
会显示为“00:00”。 - shaedrich作为工作要求,我需要这样做以显示这种格式的小时数。 起初我尝试了这个方法。
moment.utc(totalMilliseconds).format("HH:mm:ss")
但是超过24小时后,小时数将重置为0。但分钟和秒钟是准确的。因此,我仅使用了这部分来计算分钟和秒钟。
var minutesSeconds = moment.utc(totalMilliseconds).format("mm:ss")
var hours = moment.duration(totalMilliseconds).asHours().toFixed()
为了得到我们想要的格式,我们只需要把它粘在一起。
var formatted = hours + ":" + minutesSeconds
totalMilliseconds
是894600000
,则返回249:30:00
。
希望这有所帮助。在评论区留下任何问题。;)我使用:
var duration = moment.duration("09:30");
var str = moment(duration._data).format("HH:mm");
我得到了变量str中的"09:30"。
moment(moment.duration(1200000)._data).format('hh:mm:ss')
将返回12:20而不是00:20。 - Anna Rmoment.utc(...).format("HH:mm");
。 - Gil Epshtain
HH:mm:ss
会给出00:00:23
。所以只是小时部分的大小写错误。 - goldenratiomoment.duration(diff_ms).humanize();
- Rafe