# DateTime Format to string?

**URL:** <https://forum.elixirforum.com/t/datetime-format-to-string/1698>\
**Category:** Questions\
**Tags:** datetime\
**Created:** [September 18, 2016, 4:18am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698 "2016-09-18T04:18:26Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![electic](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@electic](https://forum.elixirforum.com/u/electic)\
**Post date:** [September 18, 2016, 4:18am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/1 "2016-09-18T04:18:26Z")

</div>

Hi,

I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to format a datetime to a string. Can someone point me in the right direction?

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [September 18, 2016, 6:10am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/2 "2016-09-18T06:10:12Z")

</div>

You can do that

```elixir
iex(1)> d = DateTime.utc_now
%DateTime{calendar: Calendar.ISO, day: 18, hour: 6, microsecond: {876000, 6},
 minute: 8, month: 9, second: 59, std_offset: 0, time_zone: "Etc/UTC",
 utc_offset: 0, year: 2016, zone_abbr: "UTC"}
iex(2)> DateTime.to_string(d)
"2016-09-18 06:08:59.876000Z" 
iex(3)> s = "#{d.year}/#{d.month}/#{d.day}"
"2016/9/18"

```

---

<div class="post-metadata">

**Author:** ![electic](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@electic](https://forum.elixirforum.com/u/electic)\
**Post date:** [September 18, 2016, 6:15am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/3 "2016-09-18T06:15:47Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![kujua](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kujua/32/1446_2.png) [@kujua](https://forum.elixirforum.com/u/kujua)\
**Post date:** [September 18, 2016, 6:32am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/4 "2016-09-18T06:32:50Z")

</div>

Edited to clarify:

Another way is - using this [Calendar library](https://github.com/lau/calendar):

```
iex(1)> nbo = Calendar.DateTime.now! "Africa/Nairobi"
  %DateTime{calendar: Calendar.ISO, day: 23, hour: 12, microsecond: {149378, 6},
    minute: 31, month: 7, second: 54, std_offset: 0, time_zone: "Africa/Nairobi",
    utc_offset: 10800, year: 2016, zone_abbr: "EAT"}
iex(2)> nbo |> Calendar.DateTime.shift_zone!("Europe/Vienna")
            |> Calendar.DateTime.Format.iso8601 
  "2016-07-23T11:31:54.149378+02:00"

```

First we create a timezone for Nairobi. The library uses the names in this [list](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), so we define Africa/Nairobi as timezone name.  
On the line iex(2) is the command to get the current date and time formatted as universal date time. We convert this into the Europe/Vienna timezoneand then we format the date and time to ISO 8601. There are more than 20 formats to choose from and if this is not covering your case it is possible to define custom formats.

The library must be loaded, for example by adding a dependency to mix.exs ({:calendar, “~\> 0.16.0”}) and then run iex -S mix.

From my book [Erlang and Elixir for Imperative Programmers](http://leanpub.com/erlangandelixirforimperativeprogrammers), page 120.

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [September 18, 2016, 6:43am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/5 "2016-09-18T06:43:29Z")

</div>

> [@kujua](#):
>
> nbo = Calendar.DateTime.now! “Africa/Nairobi”

But that won’t work out of the box, because Calendar is behaviour [http://elixir-lang.org/docs/stable/elixir/Calendar.html](http://elixir-lang.org/docs/stable/elixir/Calendar.html) and without defining callbacks by you or any other model it won’t work.

```elixir
iex(4)> d = Calendar.DateTime.now! "Africa/Nairobi"
** (UndefinedFunctionError) function Calendar.DateTime.now!/1 is undefined (module Calendar.DateTime is not available)
    Calendar.DateTime.now!("Africa/Nairobi")

```

---

<div class="post-metadata">

**Author:** ![kujua](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kujua/32/1446_2.png) [@kujua](https://forum.elixirforum.com/u/kujua)\
**Post date:** [September 18, 2016, 6:49am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/6 "2016-09-18T06:49:47Z")

</div>

Correct.

Calendar must have been loaded before.

---

<div class="post-metadata">

**Author:** ![electic](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@electic](https://forum.elixirforum.com/u/electic)\
**Post date:** [September 18, 2016, 6:51am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/7 "2016-09-18T06:51:27Z")

</div>

Question, by loading it before I assume you mean using the hex module Calendar right?

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [September 18, 2016, 6:53am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/8 "2016-09-18T06:53:24Z")

</div>

I just realized @kujua were talking about a third party library [GitHub - lau/calendar: date-time and time zone handling in Elixir · GitHub](https://github.com/lau/calendar), and not about the Elixir’s standard library Calendar [http://elixir-lang.org/docs/stable/elixir/Calendar.html](http://elixir-lang.org/docs/stable/elixir/Calendar.html). I think he should mention about what library he was writing in his response, to avoid confusion.

---

<div class="post-metadata">

**Author:** ![kujua](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kujua/32/1446_2.png) [@kujua](https://forum.elixirforum.com/u/kujua)\
**Post date:** [September 18, 2016, 7:02am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/9 "2016-09-18T07:02:05Z")

</div>

Sorry for being not clear - was a long evening yesterday 🙂

The mix file has a dependency {:calendar, “~\> 0.16.0”}  
Starting in the directory where the file resides: iex -S mix. This will load all dependencies.

As @sztosz rightly says this is the library [https://github.com/lau/calendar](https://github.com/lau/calendar) which has more features than the built-in one in 1.3. Also, version 0.16.0 only works with Elixir 1.3, because it accepts the built-in calendar 1.3 types.

---

<div class="post-metadata">

**Author:** ![yurko](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/yurko/32/2369_2.png) [@yurko](https://forum.elixirforum.com/u/yurko)\
**Post date:** [September 18, 2016, 8:32am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/10 "2016-09-18T08:32:33Z")

</div>

Erlang’s calendar is also good, I use it to get string dates with flexible format like that:

```
defmodule Util.Time do
  @moduledoc """
  Time helper functions.
  """

  @doc """
  Get current time as string using Erlangs calendar module.
  """
  @spec now_to_string() :: String
  def now_to_string() do
    {{year, month, day}, {hour, minute, second}} = :calendar.local_time()
    "#{day}.#{month |> zero_pad}.#{year} #{hour |> zero_pad}:#{minute |> zero_pad}:#{second |> zero_pad}"
  end

  @spec zero_pad(Integer, Integer) :: String
  defp zero_pad(number, amount \\ 2) do
     number
     |> Integer.to_string
     |> String.rjust(amount, ?0)
  end

end

```

---

<div class="post-metadata">

**Author:** ![electic](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@electic](https://forum.elixirforum.com/u/electic)\
**Post date:** [September 18, 2016, 6:59pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/11 "2016-09-18T18:59:01Z")

</div>

@kujua I went with the Calendar module. The inbuilt one seemed pretty simple and didn’t do diffs, etc. Thanks for everyone’s help!

---

<div class="post-metadata">

**Author:** ![alxvallejo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alxvallejo/32/12532_2.png) [@alxvallejo](https://forum.elixirforum.com/u/alxvallejo)\
**Post date:** [November 29, 2018, 9:36pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/12 "2018-11-29T21:36:58Z")

</div>

How would you format it though?

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [November 29, 2018, 9:37pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/13 "2018-11-29T21:37:22Z")

</div>

Can you elaborate?

---

<div class="post-metadata">

**Author:** ![kodepett](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kodepett/32/13484_2.png) [@kodepett](https://forum.elixirforum.com/u/kodepett)\
**Post date:** [November 1, 2019, 6:53pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/14 "2019-11-01T18:53:54Z")

</div>

How about a format like 20191101T17:01:58+0000 - I derived this in PHP using `Ymd\TH:i:sO` format.

---

<div class="post-metadata">

**Author:** ![tme\_317](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/0ea827/32.png) [@tme\_317](https://forum.elixirforum.com/u/tme_317)\
**Post date:** [November 1, 2019, 7:25pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/15 "2019-11-01T19:25:53Z")

</div>

I would use the Calendar library (mentioned earlier in this thread) with the `strftime` function to customize the DateTime formats however you like.

> **[Calendar.Strftime — Calendar v1.0.0](https://calendar.hexdocs.pm/Calendar.Strftime.html)**

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [November 2, 2019, 7:35am UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/16 "2019-11-02T07:35:38Z")

</div>

There‘s also a standalone strftime implemention:  
[https://github.com/plataformatec/nimble\_strftime](https://github.com/plataformatec/nimble_strftime)

Most things `calendar` did found it‘s way into elixir core in the last few versions, so I tend to suggest it (as well as `timex`) less and less.

---

<div class="post-metadata">

**Author:** ![alexandre](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alexandre/32/678_2.png) [@alexandre](https://forum.elixirforum.com/u/alexandre)\
**Post date:** [May 1, 2025, 6:58pm UTC](https://forum.elixirforum.com/t/datetime-format-to-string/1698/17 "2025-05-01T18:58:58Z")

</div>

Here: [Calendar — Elixir v1.20.2](https://hexdocs.pm/elixir/Calendar.html#strftime/3)
