Created
December 9, 2013 17:12
-
-
Save sinfante/7876093 to your computer and use it in GitHub Desktop.
Helper to show distance between 2 DateTime.
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
module TimeDistanceHelper | |
def date_time_distance(start_date_time,end_date_time) | |
seconds_distance = (end_date_time - start_date_time).to_i.abs | |
days_distance = (seconds_distance / (3600 * 24)).to_i | |
seconds_distance -= days_distance * 3600 * 24 | |
hours_distance = (seconds_distance / 3600).to_i | |
seconds_distance -= hours_distance * 3600 | |
minutes_distance = (seconds_distance / 60).to_i | |
seconds_distance -= minutes_distance * 60 | |
if days_distance >= 10 | |
"#{days_distance}d" | |
elsif days_distance > 0 | |
"#{days_distance}d #{hours_distance}h" | |
elsif hours_distance >= 10 | |
"#{hours_distance}h" | |
elsif hours_distance > 0 | |
"#{hours_distance}h #{minutes_distance}m" | |
elsif minutes_distance > 0 | |
"#{minutes_distance}m" | |
else | |
"-" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment