将时间间隔的秒数格式设置为X小时Y分钟Z秒 [英] Format time interval in seconds as X hour(s) Y minute(s) Z second(s)
本文介绍了将时间间隔的秒数格式设置为X小时Y分钟Z秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以采用倒数计时格式格式化秒?例如,如果我有 var seconds = 3662
,如何显示: 1小时1分2秒
.
Is it possible to format seconds in a countdown style? For example, if I have var seconds = 3662
, how can I show: 1 hour 1 minute 2 seconds
.
我尝试使用 formatDistanceStrict(3662)
,但这只是打印小时: 1小时
.
I tried using formatDistanceStrict(3662)
but this is only printing the hour: 1 hour
.
是否有内置的方法可以显示分钟和秒钟?我不想编写100行代码来使它正常工作(就像来自互联网的其他示例一样)
Is there a built-in method to show the minutes and seconds too? I don't want to write 100 lines of code to get this working (like other examples from internet)
推荐答案
使用date-fns库,我能找到的最简单的解决方案是:
The simplest solution I could find, using the date-fns library, is this:
import { formatDuration, intervalToDuration } from 'date-fns';
function humanDuration(time: number) {
return formatDuration(intervalToDuration({start: 0, end: time * 1000}));
};
humanDuration(463441); // 5 days 8 hours 44 minutes 1 second
这篇关于将时间间隔的秒数格式设置为X小时Y分钟Z秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文