Convert Duration in milliseconds to weeks

Hello, I am using the Timex module and have a duration denominated in miliseconds that I would like to convert into weeks. I am using the format_duration function which allows me to present it as a human-readable text but for the life of me cannot figure out how to convert it to weeks so that is more human readable.

So you have a duration from milliseconds, so something like this as an example?

iex(devserver2@127.0.0.1)38> d = Timex.Duration.from_microseconds(5462387375826)
#<Duration(P2M3DT5H19M47.375826S)>

And you want to format it to just show weeks? Duration’s don’t have an std-style formatter like DateTime’s do but it wouldn’t be hard to code it yourself:

iex(devserver2@127.0.0.1)39> "#{Timex.Duration.to_weeks(d)} weeks"
"9.03172515844246 weeks"

Or if you want it in integral form:

iex(devserver2@127.0.0.1)40> "#{Timex.Duration.to_weeks(d) |> trunc()} weeks"
"9 weeks"

Personally I keep it as the ISO standard format, but humanized is useful on occasions, and it’s pretty easy to make your own via it’s Timex.Duration.to_* functions. :slight_smile:

3 Likes