Achintha Samindika Achintha Samindika - 1 year ago 871
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");

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download