views:

261

answers:

1

I am trying to use Jquery to display dates in a dropdown with interval of half month... so the first value would be the coming month's 1st, then second will be the coming month's 15th and third value would be next to next month's first and so on...

If today date is less than 15th then the first value would be the 15th of current month.

What will be the best or a cleaner way to do this... (want to display in the dropdown)

Thanks

A: 

Something like this would work:

var date = new Date();
if (date.getDate() == 1){
    date.setDate(1);
} else {
    date.setDate(15);
}
var options = [];
for(var i=0; i<15; i++){
    var year = date.getFullYear(),
        month = date.getMonth(),
        day = date.getDate(),
        out = month+'/'+day+'/'+year;

    options.push('<option value="'+out+'">'+out+'</option>');
    if (day == 1){
        date.setDate(15);
    } else {
        date.setDate(1);
        date.setMonth(month+1);
    }
}
$("#date_select").append(options.join(''));

Html would be:

<select id="date_select" name="date_select"></select>
PetersenDidIt