Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a date-time using Timex

I have a date. How can I format it via Times so that it becomes of the format:

Wed, 02 Oct 2002 15:00:00 +0200

or

Wed, 02 Oct 2002 15:00:00 GMT

or

Wed, 02 Oct 2002 15:00:00 EST

I've tried this:

 Timex.format!(my_date, "%D, %d %M %Y %H:%i:%s T", :strftime))

but it threw an exception:

%Timex.Format.FormatError{message: {:format, "Expected end of input at line 1, column 16"}} (expected a string)

while it's getting converted into other, simpler, formats with no errors.

like image 510
Kuqa Avatar asked Mar 09 '23 16:03

Kuqa


2 Answers

I believe you're looking for RFC1123 DateTime format:

iex(1)> Timex.now |> Timex.format!("{RFC1123}")
"Sat, 11 Mar 2017 12:04:21 +0000"
iex(2)> Timex.now |> Timex.to_datetime("America/Chicago") |> Timex.format!("{RFC1123}")
"Sat, 11 Mar 2017 06:04:50 -0600"
like image 67
Dogbert Avatar answered Mar 15 '23 14:03

Dogbert


Timex.now()|> Timex.format!("%Y-%m-%d %H:%M", :strftime)

output: 2019-05-23 11:06

like image 45
zhulinpinyu Avatar answered Mar 15 '23 14:03

zhulinpinyu