jquery ui datepicker back button jumps to 1899

2019-04-28 07:46发布

问题:

I must be missing some setting or something, but when I use the back button the datepicker jumps to 1899 from the current year.

Has anyone else seen this and fixed it?

(You can see the different combos I've commented out.)

    $('.dialog-search,#from')
   .datepicker({
    defaultDate: 'c-1m',
    dateFormat: 'yymmdd',
    //changeMonth: true,
    changeYear: true,
    showAnim: 'fadeIn',
    duration: 200,
    //gotoCurrent: true, 
    showOtherMonths: true,
    selectOtherMonths: true,
    currentText: 'Now',
    shortYearCutoff: '+20',
    //showWeek: true,
    //firstDay: 1,
    numberOfMonths: 1,
    showButtonPanel: true,
    yearRange: 'c-200:c',
    onSelect: function(selectedDate,inst) {
     $('.dialog-search,#from').val(selectedDate);
    }
   });

回答1:

I had this issue when I had multiple text inputs with the same ID, which is easy to do with MVC frameworks when you have multiple editors on page for same model class. I resolved it by explicitly specifying Id and leaving Name as is, so that model resolving works on postback.



回答2:

This was caused by having multiple input fields with the same id, in my current project.

I was generating multiple forms at the front end, with the same creation method in my framework.

To avoid having to write explicit changes at the form creation level, I did the following:

//An initialisation method for datepickers
var init_datepickers = function() {
  /* Datepickers */
  $('.datepicker').datepicker({
    dateFormat: "dd/mm/yy" //(I'm in NZ)
  });
}
//Define counter ("datepicker index" aka dpi)
var dpi = 0;
//Loop datepickers
$.each($('input.datepicker'), function() {
  if(dpi > 0)
    $(this).attr("id", $(this).attr("id") + "_" + dpi); //Append the current count to the ID
  dpi++;
  //Check for last datepicker, and call init
  if(dpi == $('input.datepicker').length)
    init_datepickers();
});

This ads a number to all datepickers, other than the first one that appears on the page.

Note: This example doesn't really call for the use of ID's as selectors for scripts or styling. This will change the ID's of the inputs, and therefor, will break any styling / selection that you may have.



回答3:

I experienced this issue when calling

$(...).datepicker('destroy')

before re-initializing the datepicker.



回答4:

It happens when if you have a default instance, because in jQuery plugin its configured that default date is 1899.

We have to just find out why this when this default date code fires



回答5:

I have created a demo with your code in jsfiddle and I can't reproduce your problem. Can you check it at http://jsfiddle.net/w78xX/.



回答6:

I have same issue. I'm populating datepicker fields with ajax and showing them on PrettyPhoto as popup.

Here is my code.

$( "body" ).on( "focus", ".tarih", function() {
    //$('.tarih').datepicker('destroy');
    var i = 0;
    $('.tarih').each(function () {
        //$(this).attr("id",'date' + i).datepicker('destroy');
        $(this).attr("id",'date' + i).datepicker({
            dateFormat: "yy-mm-dd",
            firstDay: 1,
            minDate: new Date(2000, 1 - 1, 1)
        });
        i++;
    });
});


回答7:

Try using: format: 'mm/dd/yyyy hh:ii:ss P' The trick is to keeping your date format the same as what the back-end returns.



回答8:

I had the same issue and adding the useCurrent: false resolves it.

.datepicker({
        useCurrent: false 

    });