matthewb
matthewb

Reputation: 3476

Round numbers in 15min javascript

I am trying to do a select box with 4 options 00,15,30,45

I want to take the current time and round it to 15 min increments, and have the value change.

I have

current_min = start_date.getMinutes();
$('#event-hour').val(current_min);

I played with this roundedMinutes=(15*Math.floor(enteredMinutes/15)) but i couldn't get it to work right.

Upvotes: 0

Views: 1540

Answers (2)

CodeJoust
CodeJoust

Reputation: 3810

currentTimeRounded = (15*Math.round(date.getMinutes()/15));

js> (15*Math.round(date.getMinutes()/15));
15

Works just fine for me.

Upvotes: 2

jason
jason

Reputation: 8918

Use Math.round instead of Math.floor and everything should be ok-- other than that, your equation for rounding to the nearest n is correct.

Upvotes: 4

Related Questions