module Timely::String
Public Instance Methods
to_date(fmt = nil)
click to toggle source
fmt e.g. '%d/%m/%Y' By default it will try to guess the format If using ActiveSupport you can pass in a symbol for the DATE_FORMATS
# File lib/timely/string.rb, line 8 def to_date(fmt = nil) if fmt fmt = Date::DATE_FORMATS[fmt] if fmt.is_a?(Symbol) && defined?(Date::DATE_FORMATS) parsed = ::Date._strptime(self, fmt) parsed[:year] = parsed[:year] + 2000 if parsed[:year] < 1000 ::Date.new(*parsed.values_at(:year, :mon, :mday)) else ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday)) end rescue NoMethodError, ArgumentError raise DateFormatException, "Date #{self} is invalid or not formatted correctly." end