Achintha Samindika Achintha Samindika - 1 month ago 28x
Javascript Question

Round moment.js object time to nearest 30 minute interval

I was trying to round the moment.js time object to next nearest 30 minute interval. But looks my logic us wrong. Can someone help me.

10:13am -> 10:30am
11:45am -> 12:00pm

Here is my current code

start = moment();
minuteReminder = start.minute() % 30;
start.add(minuteReminder, 'minutes');
start.format("D YYYY, h:mm:ss a");

start = moment("2014/08/15 11:00");
remainder = (30 - start.minute()) % 30;
moment(start).add("minutes", remainder ).format("D YYYY, h:mm:ss a");

Million ways to do this. You don't need moment.js really. Anyway, here is one.