简体   繁体   中英

jQuery UI Datepicker separate date/month+year

I want a datepicker like the one on booking.com.

It automatically changes the day+date of the month when a month+year in another select box is changed. I have had a look at the API of jQuery datepicker but I couldn't find anything that would help me.

See i Created One Fiddle Where you Select Date From DatePicker then it will change in Day/Month/year on Another Field . : Demo

$(function() {
$("#fullDate").datepicker({
    onClose: function(dateText, inst) {
        $('#year').val(dateText.split('/')[2]);
        $('#month').val(dateText.split('/')[0]);
        $('#day').val(dateText.split('/')[1]);
    }
});
});

Thanks for your input. I finally figured how to get it working.

So here is the answer for anyone else who wants this kind of functionality.

HTML

<select name="day" id="day">
     <option value="">Select Day</option>
</select>
<select name="month" id="month">
     <option value="">Select Month</option>
</select>

JS

function getMonths() {
    var months = new Array(12);
    months[0] = "Jan";
    months[1] = "Feb";
    months[2] = "Mar";
    months[3] = "Apr";
    months[4] = "May";
    months[5] = "Jun";
    months[6] = "Jul";
    months[7] = "Aug";
    months[8] = "Sep";
    months[9] = "Oct";
    months[10] = "Nov";
    months[11] = "Dec";
    var d = new Date();
    var month = d.getMonth();
    var year = d.getFullYear();
    var monthsArray = new Array(13);
    for (var i = 0; i < 13; i++) {
        var now = new Date();
        if (now.getMonth() == 11) {
            var current = new Date(now.getFullYear() + 1, 0, 1);
        } else {
            var current = new Date(now.getFullYear(), now.getMonth() + i, 1);
        }
        monthsArray[current.getMonth() + 1 + '_' + current.getFullYear()] = months[current.getMonth()] + ' ' + current.getFullYear();
    }
    return monthsArray;
}

function getDaysInMonth(month, year, daySelector) {
    var names = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    var date = new Date(year, month - 1, 1);
    var days = [];
    while (date.getMonth() == month - 1) {
        days[date.getDate()] = (date.getDate() + " " + names[date.getDay()]);
        date.setDate(date.getDate() + 1);
    }
    daySelector.find('option').remove();
    daySelector.append($("<option></option>").attr("value", '').text("Day"));
    for (var key in days) {
        daySelector.append($("<option></option>").attr("value", key).text(days[key]));
    }
}

var months = getMonths();
for (var key in months) {
    $('#month')
        .append($("<option></option>")
        .attr("value", key)
        .text(months[key]));
}

$('#month').change(function () {

    var monthYear = $('#month').val().split('_');
    getDaysInMonth(monthYear[0], monthYear[1], $('#day'));

});

Demo & Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM