Last active
August 29, 2015 13:55
-
-
Save matt-shipman/8732888 to your computer and use it in GitHub Desktop.
This method takes in a integer or decimal value of time remaining and converts it to natural language. I was unable to find a decent gem so I decided to whip this up quickly. An example of how to use this method: time_to_natural_language(20.days.from_now - Time.now). This method should go in your items_helper.rb or application_helper.rb
This file contains hidden or 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 time_to_natural_language(time_left) | |
| if time_left > 0 | |
| time_left = time_left.round.to_i | |
| weeks = time_left / 604800 | |
| time_left -= weeks * 604800 | |
| days = time_left / 86400 | |
| time_left -= days * 86400 | |
| hours = time_left / 3600 | |
| time_left -= hours * 3600 | |
| minutes = time_left / 60 | |
| time_left -= minutes * 60 | |
| seconds = time_left | |
| weeks = weeks.to_s | |
| days = days.to_s | |
| hours = hours.to_s | |
| minutes = minutes.to_s | |
| seconds = seconds.to_s | |
| if weeks.to_i > 0 | |
| pluralize(weeks, 'week') | |
| elsif days.to_i > 0 | |
| pluralize(days, 'day') | |
| elsif hours.to_i > 0 | |
| pluralize(hours, 'hours') + ", " + pluralize(minutes, 'minute') | |
| else | |
| pluralize(minutes, 'minute') + ", " + pluralize(seconds, 'second') | |
| end | |
| else | |
| "Auction end" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment