Eli Stone Eli Stone - 6 months ago 48
Javascript Question

Javascript: Round Time UP nearest 5 minutes

I need to be able to round time to the next nearest 5 minutes.

e.g.

Time now 11:54 - clock is 11:55
Time now 11:56 - clock is 12:00

It can never round down just always up to the next time.

I am using this code at the moment but this will round down as well

var time = 1000 * 60 * 5;
var date = new Date();
var rounded = new Date(Math.round(date.getTime() / time) * time);

Answer

Add 2.5 minutes to your time, then round.

11:54 + 2.5 = 11:56:30 -> 11:55
11:56 + 2.5 = 11:58:30 -> 12:00