You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a user types a 2-digit year which is then passed in to new Date(), Javascript creates a Date in the 1900's.
Seriously. It's even written into the doc for the Date constructor:
Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year.
For example, 04/19/09 turns into April 19, 1909.
Since it's 2020 today and 1909 is a long time ago, my assumption was that the DatePicker example should move all 2-digit dates into the 2000's, and @jongund added some logic to do that in #1362 (Thank-you, Jon).
However, what if today's user types 99? I wonder if they are more likely to be thinking about 1999 than 2099?
Spitballing, perhaps a typical user in a typical app might expect a 2-digit year to end up somewhere between 80 years into the past (i.e. birthdays, historic events) and 20 years into the future (i.e. medium to long-term planning)?
Obviously, though, it depends on the app. If the question is "When were you born?", then future dates are not useful. If the question is "When do you plan to travel?", then past dates don't make sense.
For the APG's DatePicker example, however, something like the following might be generically useful (this implements the 80/20 logic explained above, and it is future-proof, i.e. I tested it with thisYear set to some future years as well):
DatePickerDialog.prototype.getDateFromTextbox=function(){varparts=this.textboxNode.value.split('/');varmonth=parseInt(parts[0]);varday=parseInt(parts[1]);varyear=parseInt(parts[2]);vartoday=newDate();if((parts.length===3)&&Number.isInteger(month)&&Number.isInteger(day)&&Number.isInteger(year)){if(year<100){// calculate fallback value for 2-digit yearvarthisYear=today.getFullYear();varthisCentury=Math.trunc(thisYear/100)*100;year=thisCentury+year;// start in this centuryif(year>thisYear+20){// if more than 20 years in the future, assume user meant last centuryyear=year-100;}elseif(year<=thisYear-80){// if more than 80 years in the past, assume user meant next centuryyear=year+100;}}this.focusDay=newDate(year,month-1,day);this.selectedDay=newDate(this.focusDay);}else{// If not a valid date (MM/DD/YYYY or YY) initialize with today's datethis.focusDay=today;this.selectedDay=newDate(0,0,1);}};
Low priority to fix this - I just wanted to make a note of it in case anyone trips over it.
If folks agree with the logic, I could create a PR.
The text was updated successfully, but these errors were encountered:
If a user types a 2-digit year which is then passed in to new Date(), Javascript creates a Date in the 1900's.
Seriously. It's even written into the doc for the Date constructor:
For example, 04/19/09 turns into April 19, 1909.
Since it's 2020 today and 1909 is a long time ago, my assumption was that the DatePicker example should move all 2-digit dates into the 2000's, and @jongund added some logic to do that in #1362 (Thank-you, Jon).
However, what if today's user types 99? I wonder if they are more likely to be thinking about 1999 than 2099?
Spitballing, perhaps a typical user in a typical app might expect a 2-digit year to end up somewhere between 80 years into the past (i.e. birthdays, historic events) and 20 years into the future (i.e. medium to long-term planning)?
Obviously, though, it depends on the app. If the question is "When were you born?", then future dates are not useful. If the question is "When do you plan to travel?", then past dates don't make sense.
For the APG's DatePicker example, however, something like the following might be generically useful (this implements the 80/20 logic explained above, and it is future-proof, i.e. I tested it with thisYear set to some future years as well):
Low priority to fix this - I just wanted to make a note of it in case anyone trips over it.
If folks agree with the logic, I could create a PR.
The text was updated successfully, but these errors were encountered: