Ruby Duration

Download ruby duration lib recieves a string describing a duration of time, and converts it into: seconds, and a standard readable format.
Or, you can pass an integer representing seconds, and receive the same standard readable format.


Passing a String

duration ="2weeks 8 hr 30m")
duration.seconds  # => 1240200
duration.readable  # => "2 weeks, 8 hours and 30 mins"

Passing an Fixnum (of seconds)

duration =
duration.readable  # => "2 weeks, 8 hours and 30 mins"


Example valid input strings:

“2 weeks”, “2wks”, “2w”,
“8h”, “8 hrs”,”8 hours”,
“30m”, “0:30″, “30 minutes”, “30min”,
“2 weeks, 8 hours and 30 minutes”, “2w 8h 30m”, “2w 8:30″,
“4 hours 30 minutes”, “4 hours and 30m”, “4h 30min”, “4:30″, “4.5”, “4.50”, “4h, 30 min”, “4.5 hours”, “4.50h”


ahabman added functionality, extended and mashed-up this stack overflow article and english friendly timespan

This entry was posted in ruby. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Trackback

  1. […] to install the following gems (”sudo gem install FOO”): map_by_method, what_methods, duration, and wirble. Most options should be self-explanatory and there’s plenty of […]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>