[英]using moment.js to display duration in format 00:XX when duration is less than an hour
I'm trying to format a countdown timer using moment duration format but as soon as time hits below 60 minutes , the hours disappear eg 60 minutes display as "01:00" which is correct 59 minutes display as "59" which is not correct, it should display as "00:59" 我正在尝试使用时刻持续时间格式来设置倒计时器的格式,但是一旦时间低于60分钟,则小时消失,例如60分钟显示为“01:00”,这是正确的59分钟显示为“59”,这是不正确的,它应显示为“00:59”
where 3500000 milliseconds is equal to 58 minutes 其中3500000毫秒等于58分钟
I have the following code: 我有以下代码:
moment.duration(3500000).format("hh:mm", { forceLength: true })
which displays result : 58, rather than 00:58 显示结果:58,而不是00:58
what am I doing wrong here ? 我在这做错了什么?
I have also attempted the variation to no avail 我也尝试过这种变化无济于事
moment.duration(3500000).format("HH:mm", { forceLength: true })
I can explain what's happening (I created the moment-duration-format
plugin). 我可以解释发生了什么(我创建了moment-duration-format
插件)。
The forceLength
option only affects the first token that has a value , meaning the first token with a value greater than 0
. forceLength
选项仅影响具有值的第一个标记,这意味着第一个标记的值大于0
。 In your case, the hh
token doesn't have a value. 在您的情况下, hh
标记没有值。 https://github.com/jsmreese/moment-duration-format#force-length https://github.com/jsmreese/moment-duration-format#force-length
Switching from hh
to HH
means something for formatting moment objects (dates), but not for formatting moment duration objects (lengths of time) with my plugin (unless you've customized the duration formatting tokens, which is possible using my plugin). 从hh
切换到HH
意味着用于格式化时刻对象(日期),但不是用于使用我的插件格式化时刻持续时间对象(时间长度)(除非您已经自定义了持续时间格式化令牌,这可以使用我的插件)。
Using moment(moment.duration(3500000)._data).format("HH:mm");
使用moment(moment.duration(3500000)._data).format("HH:mm");
as suggested is a nice creative workaround. 建议是一个很好的创意解决方法。
If you want to grab the version of moment-duration-format that's on the repository's dev
branch, there is an option that can help (see https://github.com/jsmreese/moment-duration-format/issues/22 )... 如果你想获取存储库的dev
分支上的moment-duration-format版本,有一个选项可以提供帮助(参见https://github.com/jsmreese/moment-duration-format/issues/22 )。 ..
In that version you can use the *
character to denote the minimum token to show while trimming, even with it has no value: 在该版本中,您可以使用*
字符表示修剪时显示的最小标记,即使它没有值:
moment.duration(3510000).format("*hh:mm");
--> "00:59"
moment.duration(3509999).format("*hh:mm");
--> "00:58"
Note that the default behaviour in the dev
branch version changed from truncate
to round
so you'll drop from 00:59
to 00:58
as you pass from 58 minutes 30 seconds
to 58 minutes 29 seconds
. 请注意, dev
分支版本中的默认行为从truncate
更改为round
因此当您从58 minutes 30 seconds
传递到58 minutes 29 seconds
,您将从00:59
下降到00:58
。 In that version you could turn on the trunc
option for this output: 在该版本中,您可以为此输出启用trunc
选项:
moment.duration(3539999).format("*hh:mm", { trunc: true });
--> "00:58"
moment.duration(3540000).format("*hh:mm", { trunc: true });
--> "00:59"
Not sure if that's what you would want for your countdown solution... maybe a feature of setting floor
( trunc
), ceiling
, or round
on the remainder would be best? 不确定这是否是你想要的倒计时解决方案...也许在剩余部分上设置floor
( trunc
), ceiling
或round
功能最好?
If you wanted a ceiling behaviour, you could use the dev
branch version along with trunc
and add 60000
to your timer value: 如果你想要一个天花板行为,你可以使用dev
分支版本以及trunc
并将60000
添加到你的计时器值:
moment.duration(3540000 + 60000).format("*hh:mm", { trunc: true });
--> "01:00"
moment.duration(3539999 + 60000).format("*hh:mm", { trunc: true });
--> "00:59"
moment.duration(3500000 + 60000).format("*hh:mm", { trunc: true });
--> "00:59"
moment.duration(3480000 + 60000).format("*hh:mm", { trunc: true });
--> "00:59"
moment.duration(3479999 + 60000).format("*hh:mm", { trunc: true });
--> "00:58"
尝试这个
moment(moment.duration(3500000)._data).format("HH:mm");
试试'trim'参数。
moment.duration(3500000).format("hh:mm", { trim: false })
My variant 我的变种
moment.utc(3400000).format("HH:mm")
gives : "00:56" 给出:“00:56”
Hope will be usefull. 希望将是有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.