Using a Date()
instance, how might I round a time to the nearest five minutes?
For example: if it's 4:47 p.m. it'll set the time to 4:45 p.m.
To round a number to the nearest 5, call the Math. round() function, passing it the number divided by 5 and multiply the result by 5 .
To round a date to the nearest hour: Use the setMinutes() method to set the minutes of the date to its current minutes + 30 . Use the setMinutes() method to set the minutes, seconds and milliseconds to 0 .
You also can use this formula =MROUND(A2,15/60/24) to round time to nearest minute.
That's pretty simple if you already have a Date
object:
var coeff = 1000 * 60 * 5; var date = new Date(); //or use any other date var rounded = new Date(Math.round(date.getTime() / coeff) * coeff)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With