Created
February 28, 2019 07:03
-
-
Save maricris-sn/99c986c1bdcbdf92d1e23baef47bce9a to your computer and use it in GitHub Desktop.
Dates To Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dates_to_time(date_string, created_at) | |
if date_string[/[0-9]+/].blank? | |
# there aren't any numbers in it | |
return 0 | |
else | |
# there's at least one number to derive date from | |
if date_string[/\'(\d){2}/].present? | |
# uses Month '14 for example | |
matched_year = date_string[/\'(\d){2}/] | |
# extrapolate the decade from when this record was created | |
decade = created_at.blank? ? "20" : created_at.strftime("%Y")[0..1] | |
return Chronic.parse(date_string.gsub("'", decade)).at_beginning_of_month.to_time.to_i | |
elsif date_string[/or|\-/].present? | |
# uses 'date - date' OR 'date or date' or 'date-date' | |
splitter = date_string[/or|\-/] | |
return Chronic.parse(date_string.split(splitter).first.strip).to_time.to_i | |
else | |
return 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the attempt to handle old, dirty dates such as: