Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def time_ago_in_words(seconds) do
minutes = round(seconds/60)
case minutes do
minutes when minutes in 0..1 ->
case seconds do
seconds when seconds in 0..4 ->
"less than 5 seconds"
seconds when seconds in 5..9 ->
"less than 10 seconds"
seconds when seconds in 10..19 ->
"less than 20 seconds"
seconds when seconds in 20..39 ->
"half a minute"
seconds when seconds in 40..59 ->
"less than 1 minute"
_ ->
"1 minute"
end
minutes when minutes in 2..44 ->
"#{minutes} minutes"
minutes when minutes in 45..89 ->
"about 1 hour"
minutes when minutes in 90..1439 ->
"about #{round(minutes/60)} hours"
minutes when minutes in 1440..2519 ->
"1 day"
minutes when minutes in 2520..43199 ->
"#{round(minutes/1440)} days"
minutes when minutes in 43200..86399 ->
"about 1 month"
minutes when minutes in 86400..525599 ->
"#{round(minutes/43200)} months"
minutes when minutes in 525600..1051199 ->
"1 year"
_ ->
"#{round(minutes/525600)} years"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment