Create DateTime from tuple


I got an embedded_schema with a field of type :utc_datetime.

I want to populate it with the ctime value from the File.Stat module which looks like {{2017, 11, 26}, {17, 49, 16}}.

But there doesn’t seem to be a built in function to create a DateTime struct from that tuple.

Any ideas?

There’s no one function for that, but you can do:

iex(3)> {{2017, 11, 26}, {17, 49, 16}} |> NaiveDateTime.from_erl!() |> DateTime.from_naive!("Etc/UTC")
#DateTime<2017-11-26 17:49:16Z>

Oh, didn’t know that NaiveDateTime had the function to parse the tuple. Thank you very much!

1 Like