Skip to content

Instantly share code, notes, and snippets.

@maricris-sn
Created February 28, 2019 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maricris-sn/99c986c1bdcbdf92d1e23baef47bce9a to your computer and use it in GitHub Desktop.
Save maricris-sn/99c986c1bdcbdf92d1e23baef47bce9a to your computer and use it in GitHub Desktop.
Dates To Time
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
@maricris-sn
Copy link
Author

In the attempt to handle old, dirty dates such as:

Oct '14 # inaccurate conversion using Chronic
April 2014 # inaccurate conversion using Chronic
3/12/14-3/12/15 # a single date should be picked
Fall 2014 # no conversion using Chronic
6/10/2014 or 6/11/2014 # a single date should be picked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment