I’m looking for something that given a date, will display the time from now in words. Example:
if today is 4/28/2012, I want:
- 4/29 to display “Tomorrow”
- 4/30 to display “Next Monday”
- 4/31 to display “Next Tuesday”
Is there a gem for this already or do I have to write it myself? If I have to write it what’s the cleanest way to do it? I haven’t written anything like this before in rails. Thanks!
I believe you’re looking for the
distance_of_time_in_wordshelper, which is already available in Rails – it’s basically the opposite oftime_ago_in_words, in the sense that it tells you the amount of time between two times, as opposed to how long since a certain time has passed vs now, and can therefore be used to measure into the future instead of into the past.It won’t give you days of the week like “Next monday”, but if you click the
Source: showlink at the bottom of the helper’s documentation, it will provide you the code you’d need to create a modified version of the helper that could display the day of week information pretty easily. Combine that with the code in this answer regarding calculating the day of the week, and you’re pretty much done.