jQuery UI DatePicker显示错误的日期

时间:2021-09-01 12:07:01

I've got a jquery UI DatePicker with the following parameters:

我有一个带有以下参数的jquery UI DatePicker:

changeMonth: true,
changeYear: true,
yearRange: "-16:-1",
dateFormat: 'dd-mm-yy'

It correctly displays only years 1996 till 2011. However, when I select a date for the first time, it's strangely displayed as 08-03-2012. 2012 is not even an option for selection in the datepicker, but this is the date which is then produced in my text box.

它正确显示1996年至2011年。然而,当我第一次选择日期时,它奇怪地显示为08-03-2012。 2012甚至不是选择日期选择器的选项,但这是在我的文本框中生成的日期。

If I then select a date once again, it's correctly displayed - this only occurs for the first time.

如果我再次选择一个日期,它会正确显示 - 这只是第一次出现。

Any ideas?

2 个解决方案

#1


5  

You can set a default date in your range like this:

您可以在此范围内设置默认日期,如下所示:

<script type="text/javascript">
$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-16:-1",
        dateFormat: 'dd-mm-yy',
        defaultDate: '01-01-1996'
    });
});
</script>

#2


0  

Here is another way to set the default date with the first year of the range.

这是另一种设置范围第一年的默认日期的方法。

<script type="text/javascript">
var default_date = new Date(); //Create a new date object
default_date.setYear(date.getYear() - 16); //Substract the current year with the first year of the range

$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-16:-1",
        dateFormat: 'dd-mm-yy',
        defaultDate: default_date 
    });
});
</script>

#1


5  

You can set a default date in your range like this:

您可以在此范围内设置默认日期,如下所示:

<script type="text/javascript">
$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-16:-1",
        dateFormat: 'dd-mm-yy',
        defaultDate: '01-01-1996'
    });
});
</script>

#2


0  

Here is another way to set the default date with the first year of the range.

这是另一种设置范围第一年的默认日期的方法。

<script type="text/javascript">
var default_date = new Date(); //Create a new date object
default_date.setYear(date.getYear() - 16); //Substract the current year with the first year of the range

$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-16:-1",
        dateFormat: 'dd-mm-yy',
        defaultDate: default_date 
    });
});
</script>