23tux
23tux

Reputation: 14726

Extend strftime directives to format a time like "tomorrow at 14:00"

I've found the list of format directives for the strftime method http://ruby-doc.org/core-2.3.0/Time.html#method-i-strftime

There are various options to format a time like weekdays, day of month... But I'm interested in formating a Time like

Time.parse("2016-02-30T12:24").strftime("%XXX, at %H:%M")
# => tomorrow at 12:24

Here %XXX should be "today", "yesterday", "tomorrow", "in 2 days" or "in 3 days".

Is it possible to extend the directives of strftime so that I can use my own placeholder?

Upvotes: 2

Views: 187

Answers (2)

Drew Reynolds
Drew Reynolds

Reputation: 70

You can use Rails Distance of Time in Words which will give you 'about 1 day' but I think it would be easier to use MomentJS's Calendar Time.

Calendar Time will help you return something like " Tomorrow at 2:30 AM"

Upvotes: 1

chaitanya
chaitanya

Reputation: 1974

I think following distance_of_time_in_words helper would help.

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

Upvotes: 1

Related Questions