electic

electic

DateTime Format to string?

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?

Most Liked

sztosz

sztosz

You can do that

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"
LostKobrakai

LostKobrakai

There‘s also a standalone strftime implemention:
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.

kujua

kujua

Author of Erlang and Elixir for Imperative Programmers

Edited to clarify:

Another way is - using this Calendar library:

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, 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, page 120.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
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 fo...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement