Convert youtube API duration to seconds in Elixir

Hey !

I want to convert the duration I get from the Youtube API:

The format is in ISO 8601 but I haven’t find a simple solution yet.

Any recommendation ?

I recommend you open the hex docs for Elixir and search for “8601” to get started.

I already tried with “PT15M33S” (example of the Youtube documentation) and it doesn’t work.

Here is what I get:

iex(8)> DateTime.from_iso8601 "PT15M33S"
{:error, :invalid_format}

I want to convert the duration I get from the Youtube API:

convert to what?

To seconds

Maybe you can search a library in hex.pm. Otherwise, you can write a little module, parsing it and convert it. It shouldnot be tricky.

Oh! That’s an ISO 8601 duration. I’ve never encountered that before. Searching hex.pm for “8601 duration” lead me to this library which looks promising, but I’ve never used it https://hex.pm/packages/duration

1 Like

This library is working properly. Thanks !