javascriptreactjspostgresqlpsqlhumanize

Round last value in humanize-duration formatted value (React)


I am setting up a table in React that applies humanize-duration to a value (the library applies ms and my data is in s, hence the multplier).

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

The value I'm getting out of this is formatted as such: 10 months, 4 weeks, 0.5540856481481482 days. My question is - how can I get that last value to act as an integer, or a shorter float at least? I've tried converting the psql vaule to integer, as seen below, with no change:

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

I've also tried setting the decimal to '', and got back the error Reference Error: decimal not defined.

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

Solution

  • One day is 24 * 60 * 60 seconds (86400), so to round to a day and convert to ms:

    Math.round(time_inventoried / 86400) * 86400000