Lunchbox
Lunchbox

Reputation: 1550

Round time interval up to 15 minute steps

I have a scenario, where the user can push a button start a stopwatch and then push it again to stop it. But there's a twist - the end time needs to be rounded up in 15 minute steps. E.g. if the start time is 08:13 and the end time 08:16, it needs to be rounded up to 08:28. Or if the interval is longer than 15 minutes like 08:31, it needs to be rounded up to 08:43.

Do any of you have any pointers of how I could tackle this situation? If what I'm asking is too complicated, how do I round up and down to the closest 15 minutes (respectively).

Upvotes: 2

Views: 986

Answers (1)

hon2a
hon2a

Reputation: 7214

This seems to be pretty simple:

var interval = 15 * 60 * 1000,    // 15 minutes in miliseconds
    roundedTime = new Date(startTime + (Math.ceil((endTime - startTime) / interval) * interval));

where startTime and endTime are Date objects.

Upvotes: 5

Related Questions