uxsolutions / bootstrap-datepicker

A datepicker for twitter bootstrap (@twbs)
Apache License 2.0
12.66k stars 6.07k forks source link

Bug? Datepicker advances to the next month in chrome #1425

Closed asunar closed 9 years ago

asunar commented 9 years ago

Type 1/1/79 in the date textbox, date picker displays January 79 Click on january 1st in the calendar, it moves (incorrectly) to feb 1st.

Details: http://screencast.com/t/BzpUGoret

hebbet commented 9 years ago

could you provide a jsfiddle?

asunar commented 9 years ago

I will try to provide a jsfiddle. However, if you watch the screencast, you can see that the I demonstrated the issue in the online demo page. http://eternicode.github.io/bootstrap-datepicker/?markup=input&format=&weekStart=&startDate=&endDate=&startView=0&minViewMode=0&todayBtn=false&clearBtn=false&language=en&orientation=auto&multidate=&multidateSeparator=&keyboardNavigation=on&forceParse=on#sandbox

asunar commented 9 years ago

I cannot repro the issue in the online demo page anymore, so it must have been fixed somehow. Were there any new releases in the last 7 days?

asunar commented 9 years ago

It only happens in chrome (Version 43.0.2357.124 m) Here is the jsfiddle http://jsfiddle.net/rn95zs5r/

Type 1/1/79 in the date textbox, date picker displays January 79 Click on january 1st in the calendar, it moves (incorrectly) to feb 1st.

Azaret commented 9 years ago

Got it, actually when fill() is called to generate the picker, it checks if the date is from this month, or the last one, or the next one, in order to know whenever if it should add the classes old and new. When UTCDate() is called for creating next/last month date, new Date return a falsy date in 1978/1979. So it assume that the day is old, then add the new class. When you click, since there is the new class, it assume it is a day from next month, so it switch to February.

The main issue here is that the Javascript Date object minimum value is 1/1/1970.

asunar commented 9 years ago

Why is it working on Firefox 38.0.5 and IE 11?

Azaret commented 9 years ago

Ok, after further investigation, the issue comes from the usage of new Date(x, x, x) that interpret year from 0 to 99 as shortcut of 1900 to 1999, yes the Y2K bug is still in the specs for some reason. The workaround if to use .setFullYear() instead of the constructor where it is still used, it can be pushed for 1.5. The reason why it seems to work in some browser or not is, I guess, either different handling of the Date constructor, or either a different path to create a date in the code. Some part of the code use .setFullYear() already, some don't. Has to be cleaned up for sure.

asunar commented 9 years ago

Thanks for investigating.