Ruby incorrectly parses 2 digit year
ruby, ruby-on-rails-3
Solution
The `strptime` method is parsing the text "63" to the year 2063, not 1963 as you want. This is because the method decides the century by using the POSIX standard.
The `chronic` gem has a similar issue because it decides the century, though differently.
A solution is to adjust the date:
d = Date.strptime("15/10/63","%d/%m/%y")
if d > Date.today
d = Date.new(d.year - 100, d.month, d.mday)
end
In the comments of this post, Stefan suggests a good one liner:
d = d.prev_year(100) if d > Date.today
If you need speed, you can try optimizing like this:
d <= Date.today || d = d << 1200
Problem
Ruby correctly parses the first date but the second one is incorrect. Tested with ruby 1.9.3 and 2.1.2. Any idea how to get it to work consistently? (We are getting in birth dates as 2 digit years) ``` Date.strptime("10/11/89","%d/%m/%y") => Fri, 10 Nov 1989 Date.strptime("15/10/63","%d/%m/%y") => Mon, 15 Oct 2063 ```