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:

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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement