Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By use moment javascript I need to add hours only (without increase or add minutes) / round to nearest hour or month or year

By use moment js, When I want to add One hour to current time, I only want to increase the hour and Not the minutes? So the time 03:25 will be 04:00 and not 04:25 (which is wrong)

// below increase 60 minutes while I only need to round to the nearest hour
$('#eventTime').val(moment().add(1, 'hours').format('HH:mm'));

Hope the solution will work also when round to nearest month (get to first day of next month) or year and so on ...

like image 439
Bashar Abu Shamaa Avatar asked Jul 21 '15 13:07

Bashar Abu Shamaa


1 Answers

Using startOf method:

moment().startOf('hour').add(1, 'hours').format('HH:mm')
like image 138
Andrey Avatar answered Oct 21 '22 05:10

Andrey