Javascript dynamic shutdown

I am not very good at handling Javascript Time.

Here's what I'm trying to do: I have a user interface that needs a drop-down list of the final time. Its final set of times is essentially from 7:00 to 22:00 in increments of 15 minutes. I use jQuery and want to create an “Select” field on the fly that looks like this:

<select>
<option value="420">7:00 AM</option>
<option value="435">7:15 AM</option>
<option value="450">7:30 AM</option>
etc...
</select>

Does anyone know how to do this in javascript / jQuery. I want to do something dynamic:

for(int i = 420; i<=1320; i+=15){
//use value of i to increment a date or something and create an option for the drop down... this is where I am stuck
}
+5
source share
1 answer

The following snippet will help you understand:

function populate(selector) {
    var select = $(selector);
    var hours, minutes, ampm;
    for(var i = 420; i <= 1320; i += 15){
        hours = Math.floor(i / 60);
        minutes = i % 60;
        if (minutes < 10){
            minutes = '0' + minutes; // adding leading zero
        }
        ampm = hours % 24 < 12 ? 'AM' : 'PM';
        hours = hours % 12;
        if (hours === 0){
            hours = 12;
        }
        select.append($('<option></option>')
            .attr('value', i)
            .text(hours + ':' + minutes + ' ' + ampm)); 
    }
}

populate('#timeSelect'); // use selector for your select
+18
source

All Articles