Ruby: how to parse the "3 days ago" string?

Given the input string 3 days ago

, how can I turn it into a date attribute?

Perhaps something like DateTime.parse('3 days ago')

?

+3


source to share


1 answer


You can get the current date as a UNIX timestamp (milliseconds or seconds since 1970 Jan 1) and then minus 3 days (also like a Unix timestamp).

i.e.



today = Time.now.to_i
three_days = 86400*3
three_days_ago_unix = today - three_days
three_days_ago_date = Time.at(three_days_ago)

      

-2


source







All Articles