I am trying to parse a string 26/03/2012, in dd/mm/yyyy format to Ruby’s Date using Date.strptime, as follows:
#!/usr/bin/ruby
require 'date'
puts 'Ruby Version: ' + RUBY_VERSION
date_str = '26/03/2012'
date = Date.strptime(date_str, "%d/%m/%y")
puts 'Parsed Date: ' + date.to_s
The output is:
Ruby Version: 1.8.7
Parsed Date: 2020-03-26
The year part has become 2020, instead of 2012!
That should be
%Yupper case, rather than%y:From the docs:
Since
%yexpects two digits only, it takes the first two20and assumes that to be a 2 digit representation of2020, since2020 % 100 = 20.