3
votes

I have a datepicker that is supposed to go from 1803 to 1995, but if you pick a day and month without choosing the year, it defaults to 2013, even though it would display 1803. How could I fix this?

Here is my code

$(function() {
        $( "#date" ).datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: "1803:1995",
          dateFormat: "yy-mm-dd",

        });
    });

I have tried using default date, but then that stops me from changing the year.

2
can u make a fiddle . - Tushar Gupta - curioustushar
"I have tried using default date, but then that stops me from changing the year." Er, no. - T.J. Crowder
"if you pick a day and month without choosing the year, it defaults to 2013, even though it would display 1803" I can replicate that: jsbin.com/UsePaPE/1/edit Recommend reporting it as a bug. - T.J. Crowder
Well, that's weird, it works now. - user2330621

2 Answers

9
votes

Yes, by default it is using the current date. To change this use either options defaultDate or setDate

$(function() {
        $( "#date" ).datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: "1803:1995",
          dateFormat: "yy-mm-dd",
          defaultDate: '1803-01-01'

        });
    });

JSFiddle

4
votes

You can use the defaultDate property, your code becomes like this

$(function() {
        $( "#date" ).datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: "1803:1995",
          dateFormat: "yy-mm-dd",
          defaultDate: "1803-01-01"

        });
    });