jQuery-UI datepicker default date

后端 未结 7 1397
盖世英雄少女心
盖世英雄少女心 2020-11-29 03:34

I have a problem with the jQuery-UI datepicker, I have searched and searched but I didn\'t find the answer. I have the following code:



        
7条回答
  •  一生所求
    2020-11-29 04:32

    If you want to update the highlighted day to a different day based on some server time, you can override the Date Picker code to allow for a new custom option named localToday or whatever you'd like to name it.

    A small tweak to the selected answer in jQuery UI DatePicker change highlighted "today" date

    // Get users 'today' date
    var localToday = new Date();
    localToday.setDate(tomorrow.getDate()+1); // tomorrow
    
    // Pass the today date to datepicker
    $( "#datepicker" ).datepicker({
        showButtonPanel: true,
        localToday: localToday    // This option determines the highlighted today date
    });
    

    I've overridden 2 datepicker methods to conditionally use a new setting for the "today" date instead of a new Date(). The new setting is called localToday.

    Override $.datepicker._gotoToday and $.datepicker._generateHTML like this:

    $.datepicker._gotoToday = function(id) {
        /* ... */
        var date = inst.settings.localToday || new Date()
        /* ... */
    }
    
    $.datepicker._generateHTML = function(inst) {
        /* ... */
        tempDate = inst.settings.localToday || new Date()
        /* ... */
    }
    

    Here's a demo which shows the full code and usage: http://jsfiddle.net/NAzz7/5/

提交回复
热议问题