.datepicker('setdate') issues, in jQuery

后端 未结 6 887
轻奢々
轻奢々 2020-12-06 04:11

I have a function that executes a query to get some data based on a certain date range which is selected using .datepicker(). I am trying to set the datepicker\'s that are

相关标签:
6条回答
  • 2020-12-06 04:22
    function calenderEdit(dob) {
        var date= $('#'+dob).val();
        $("#dob").datepicker({
            changeMonth: true,
            changeYear: true, yearRange: '1950:+10'
        }).datepicker("setDate", date);
    }
    
    0 讨论(0)
  • 2020-12-06 04:33

    If you would like to support really old browsers you should parse the date string, since using the ISO8601 date format with the Date constructor is not supported pre IE9:

    var queryDate = '2009-11-01',
        dateParts = queryDate.match(/(\d+)/g)
        realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);  
                                        // months are 0-based!
    // For >= IE9
    var realDate = new Date('2009-11-01');  
    
    $('#datePicker').datepicker({ dateFormat: 'yy-mm-dd' }); // format to show
    $('#datePicker').datepicker('setDate', realDate);
    

    Check the above example here.

    0 讨论(0)
  • 2020-12-06 04:34

    As Scobal's post implies, the datepicker is looking for a Date object - not just a string! So, to modify your example code to do what you want:

    var queryDate = new Date('2009/11/01'); // Dashes won't work
    $('#datePicker').datepicker('setDate', queryDate);
    
    0 讨论(0)
  • 2020-12-06 04:34

    Try changing it to:

    queryDate = '2009-11-01';
    $('#datePicker').datepicker({defaultDate: new Date (queryDate)});
    
    0 讨论(0)
  • 2020-12-06 04:46

    Check that the date you are trying to set it to lies within the allowed date range if the minDate or maxDate options are set.

    0 讨论(0)
  • 2020-12-06 04:49

    When you trying to call setDate you must provide valid javascript Date object.

    queryDate = '2009-11-01';
    
    var parsedDate = $.datepicker.parseDate('yy-mm-dd', queryDate);
    
    $('#datePicker').datepicker('setDate', parsedDate);
    

    This will allow you to use different formats for query date and string date representation in datepicker. This approach is very helpful when you create multilingual site. Another helpful function is formatDate, which formats javascript date object to string.

    $.datepicker.formatDate( format, date, settings );
    
    0 讨论(0)
提交回复
热议问题