Thursday, 4 July 2019

How to round a number to full minutes?

How to round seconds:

between 1 sec - 59 sec to 1 minute

between 1 min 1 sec - 1 min 59 sec --> round to 2 m

between 1h 2 min 1 sec - 2 min 59 sec --> round to 1h 3 m?

between 2 min 1 sec - 2 min 59 sec --> round to 3 m?

I use library 'moment'


Expected effect:

Date2 - date1 = The result is rounded to full minutes.

date2 - date1 = 30 s -> round to 1 m

date2 - date1 = 5s -> round to 1 m

date2 - date1 = 1m 10s -> round to 2 m

There may be a way in javascript or in the moment.js library

a = (d1, d2) => {
  let b= moment(d2).diff(d1,"minutes",true)

  return  Math.round(b) * 60; 
}

different 1m 20 s --> round to 2m --> it's ok

different 5s --> don't round to 1 m // problem here



from How to round a number to full minutes?

No comments:

Post a Comment