Bootstrap Datepicker defaults to 1900's when entered with 2 digit year

Steve Zavocki picture Steve Zavocki · Dec 11, 2013 · Viewed 10.7k times · Source

I am using a datepicker in a modal window in my Bootstrap application. I am using the original Datepicker from Stefan Petre. I built it where it works in a desktop and mobile using a mouse and it works fine.

Recently I had a user request to allow it to also work with a keyboard. I removed the readonly property to allow the user to enter a date in the input field. The date-format is set to 'mm/dd/yyyy'.

When I enter a date like today for example like this "12/11/13" then it will default to 1913. This isn't a huge deal as I could just train the users to use 4 digits, but I would rather just have it default to this century.

Note: This only seems to happen for the date-format with a 4 digit year. This also seems to happen in the same manner in the newer forks of Stefan's code.

Note: I am using Bootstrap 2.0.4. I am testing with Firefox.

Here is what it looks like:

enter image description here

Answer

Josh picture Josh · May 29, 2017

In JavaScript, set the datepicker's assumeNearbyYear attribute to true, like this:

$("#dp").datepicker({
    assumeNearbyYear: true
});