Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: string to datetime

Anyone have any luck with a String to DateTime kinda function in Elixir or Phoenix? The documentation has nothing like a parse/1 function.

Preferably from a string that looks like:

08/02/2016 6:15 PM
like image 510
t56k Avatar asked Aug 02 '16 02:08

t56k


1 Answers

There's no such function in Elixir or Phoenix. Ecto has some basic datetime parsers but they cannot parse the example string you've posted. You can use a datetime library like timex for this. Here's how you would parse the example string with timex:

iex(1)> Timex.parse("08/02/2016 6:15 PM", "{0D}/{0M}/{YYYY} {h12}:{m} {AM}")
{:ok, ~N[2016-02-08 18:15:00]}

(Swap 0D and 0M if you meant this date to parse to Aug 2 instead of Feb 8.)

like image 143
Dogbert Avatar answered Sep 18 '22 18:09

Dogbert