开发者

Jquery date in a dropdown with interval

开发者 https://www.devze.com 2022-12-22 12:23 出处:网络
I am trying to use Jquery to display dates in a dropdown wit开发者_如何转开发h interval of half month...

I am trying to use Jquery to display dates in a dropdown wit开发者_如何转开发h 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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号