How to populate two drop-downs in JQuery with to/from dates

If you ever need to populate two sets of drop-downs with the from and to hours, use the following script in your head section:

"text/javascript">
        $(document).ready(function () {
            var i = 0;

            for (var i = 0; i var ampm = i % 24 'AM' : 'PM';

                var hours = i % 12;
                if ((hours === 0) && (i!=0)) {
                    hours = 12;
                }
                var valueString = ("0" + hours.toString()).slice(-2) + " " + ampm;

                $('#ddlFromHour').append($('')
                        .attr('value', i)
                        .text(valueString));
                $('#ddlToHour').append($('')
                        .attr('value', i)
                        .text(valueString));
            }
            for (var i = 0; i var valueString = ("0" + i.toString()).slice(-2);

                $('#ddlFromMinute').append($('')
                        .attr('value', i)
                        .text(valueString));
                $('#ddlToMinute').append($('')
                        .attr('value', i)
                        .text(valueString));
            }
        });
    

Output will be two sets of drop-downs with “From and To Times”
Capture

Advertisements