Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I round minutes to the nearest 5 minute mark in momentjs?

Currently working on a site where I use moment.js. I want to use jquery only.

I get the current minute using:

var minute = moment().minute();

but I want to set an alarm starting at the nearest next 5 minute mark.

So say it's 8:38pm now, var minute would be 38. I'd want to set the input to a value of 40 (that is, 40 minutes)

So where my options are:

<select id="minuteID">
                    <option value="0">00</option>
                    <option value="5">05</option>
                    <option value="10">10</option>
                    <option value="15">15</option>
                    <option value="20">20</option>
                    <option value="25">25</option>
                    <option value="30">30</option>
                    <option value="35">35</option>
                    <option value="40">40</option>
                    <option value="45">45</option>
                    <option value="50">50</option>
                    <option value=55>55</option>
                </select>

And I can input the item like so:

var minute = moment().minute();
$("#minuteID option[value=" + minute + "]").prop("selected", "selected");

How do I make it so that instead of finding the exact option minute, it finds it at intervals of 5 (to the nearest 5 marker).

like image 474
David G Avatar asked Apr 18 '14 19:04

David G


People also ask

How do I get time from Momentjs?

You can directly call function momentInstance. valueOf(), it will return numeric value of time similar to date. getTime() in native java script.

How do you find the minutes from a date moment?

The moment(). minute() Method is used to get the minutes from the current time or to set the minutes. moment(). minutes();

How do you add minutes to a moment?

add(minutes, 'minutes') . format('LTS');


1 Answers

minute = 5 * Math.round( minute / 5 );

will do as you want.

like image 188
Bergi Avatar answered Nov 15 '22 05:11

Bergi