[英]Using Moment library, what is the correct syntax to convert minutes into days hours and minutes that also formats for singular vs plural?
Any help is greatly appreciated.任何帮助是极大的赞赏。 I've been struggling to find a solution for this question:
我一直在努力寻找这个问题的解决方案:
Moment
library, what is the correct syntax to convert minutes into days hours and minutes that also formats for singular vs plural?Moment
库,将分钟转换为天小时和分钟的正确语法是什么? expected: 2009 minutes
would become: 1 day 9 hours 29 minutes
预计:
2009 minutes
将变为: 1 day 9 hours 29 minutes
here is the incorrect code:这是不正确的代码:
function durationFormatter(minutes): string {
const ms = minutes * 60000;
const days = Math.floor(ms / 8.64e7);
const msOnLastDay = ms - days * 8.64e7;
return moment.utc(msOnLastDay)
.format("D [days] H [hours] M [minutes]");
}
console.log('durationFormatter -->', durationFormatter(2009));
The above outputs:上述输出:
1 days 9 hours 1 minutes
which is wrong 1 days 9 hours 1 minutes
这是错误的
I also tried this other moment package moment-duration-format
with this syntax: (per docs = https://github.com/jsmreese/moment-duration-format#basics )我还尝试了其他时刻 package
moment-duration-format
使用这种语法:(每个文档 = https://github.com/jsmreese/moment-duration-format#basics )
import momentDurationFormatSetup from 'moment-duration-format';
function durationFormatter(minutes): string {
momentDurationFormatSetup();
return moment.duration(minutes, "minutes").format();
}
But I get this error: Property 'format' does not exist on type 'Duration'但我收到此错误:“持续时间”类型上不存在属性“格式”
How would I use this with the package?我如何将它与 package 一起使用?
You can use the excellent library humanizeDuration , simply passing:您可以使用优秀的库humanizeDuration ,只需传递:
humanizeDuration(2009 * 60 * 1000, { delimiter: ' '})
will output what you wanted - 1 day 9 hours 29 minute.将 output 你想要什么 - 1 天 9 小时 29 分钟。
Note - you pass milliseconds, so you need to multiple the minutes parameter by 60,000 before passing to humanizeDuration注意-您传递毫秒,因此您需要将分钟参数乘以 60,000,然后再传递给 humanizeDuration
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.