Today's date in ISO 8601 format — the simplest way?

E.g., “2016-07-05”. Just the date, in a timezone of my choosing. (This is a really common requirement for working with API’s.) Here’s what I’ve come up with, in Phoenix, pulling in Timex to help.

today    =
timezone = Timex.timezone("America/Los_Angeles", today)
{:ok, today_iso_8601} = today
  |> Timezone.convert(timezone)
  |> Timex.format("%Y-%m-%d", :strftime)
1 Like

The new Calendar types can do this:

iex(6)> DateTime.to_iso8601(dt)
iex(7)> dt |> DateTime.to_iso8601
iex(8)> dt |> DateTime.to_date |> Date.to_iso8601

Thanks! Is Timex’s timezone support still the best way to do timezone conversion?

1 Like

Sorry, I’m not sure. A quick look at the Calendar source doesn’t suggest any obvious way, but I could be missing something.

I would highly recommend using the library, I’ve had a lot of success with it. If you’e already using Timex of course continue to go that route. The Elixir standard library does not handle timezones yet.