【发布时间】:2017-12-20 15:58:29
【问题描述】:
目前我的代码如下:
moment.duration(res.duration, "years").format("Y [years] M [months]"); //Input 0.08 outputs 0
当 res.duration
moment.duration(res.duration, 'months').format("M [months]"); //Input 0.08 outputs 0
但似乎还是不行。
知道如何完美地将 0.08 转换为 1 吗?
【问题讨论】:
-
我错过了什么 -
0.08指的是什么? -
所以我在我的数据库中以年为单位存储持续时间。因此,如果计算的持续时间是 1 个月,那么它将存储为 (1/12=0.08)。我将其存储到小数点后 2 位。但是为了向用户显示它,我希望它是 1(而不是 0.08),因为它更易于阅读。
-
moment.duration().format()对你有用吗? -
@ShimonBrandsdorfer 我认为 OP 正在使用 moment-duration-format 插件,将
format()方法添加到持续时间。