最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI DatePicker displaying wrong date - Stack Overflow

programmeradmin0浏览0评论

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

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.

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

Any ideas?

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

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.

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

Any ideas?

Share Improve this question asked Mar 8, 2012 at 15:29 Dot NETDot NET 4,89114 gold badges57 silver badges101 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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>

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>
发布评论

评论列表(0)

  1. 暂无评论