【问题标题】:Round last value in humanize-duration formatted value (React)人性化持续时间格式化值中的最后一个值(React)
【发布时间】:2019-05-26 16:41:57
【问题描述】:

我正在 React 中设置一个表,将humanize-duration 应用于一个值(库应用 ms,我的数据在 s 中,因此是乘数)。

<td> {humanizeDuration(time_inventoried*1000, { units: ['y', 'mo', 'w', 'd'] }) }</td>

我从中得到的值是这样格式化的:10 months, 4 weeks, 0.5540856481481482 days我的问题是 - 我怎样才能让最后一个值充当整数,或者至少是更短的浮点数? 我尝试将 psql 值转换为整数,如下所示,没有任何变化:

SELECT CAST(extract(epoch from (now() - MIN(product_selections.staged_at))) as integer) as time_inventoried

我也尝试将小数设置为'',并返回错误Reference Error: decimal not defined

  <td> {humanizeDuration(time_inventoried*1000, { units: ['y', 'mo', 'w', 'd', decimal: ''] }) }</td>

【问题讨论】:

    标签: javascript reactjs postgresql psql humanize


    【解决方案1】:

    一天是24 * 60 * 60 秒(86400),所以要舍入到一天并转换为毫秒:

    Math.round(time_inventoried / 86400) * 86400000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2014-01-16
      • 2015-05-21
      相关资源
      最近更新 更多