Dates, Time, and Formatting
Parsing Dates
Date.strptime parses text when the format is known.
Parsing Dates
parsing_dates.rb
require "date"
raw_date =
date = Date.strptime(raw_date, "%Y-%m-%d")
month = date.month
day = date.day
iso = date.iso8601
puts "raw=#{raw_date}"
puts "iso=#{iso}"
puts "month=#{month}"
puts "day=#{day}"
require "date"
raw_date =
date = Date.strptime(raw_date, "%Y-%m-%d")
month = date.month
day = date.day
iso = date.iso8601
puts "raw=#{raw_date}"
puts "iso=#{iso}"
puts "month=#{month}"
puts "day=#{day}"
require "date"
raw_date =
date = Date.strptime(raw_date, "%Y-%m-%d")
month = date.month
day = date.day
iso = date.iso8601
puts "raw=#{raw_date}"
puts "iso=#{iso}"
puts "month=#{month}"
puts "day=#{day}"
date parsing
Parsing with an explicit format avoids guessing how the input string is arranged.