I want to implement a piece of code that converts a datetime object like ~U[2022-06-07 18:37:16.842920Z]
to a format like Tue, 7 Jun 2022 18:37:16 GMT
. I do not want to use the Calendar.DateTime.Format.httpdate since our codebase already uses DateTime.
I need it to send in API headers with RFC2616 format. Any help is appreciated.
CodePudding user response:
Elixir 1.11.0 introduced Calendar.strftime/3
that is what you need. Please note that DateTime
and Calendar
are different built-in modules that serve different purposes. There should be no problem in using them combined.
~U[2022-06-07 18:37:16.842920Z]
|> DateTime.shift_zone!("Etc/UTC")
|> Calendar.strftime("%a, %-d %b %Y %X GMT")
# => "Tue, 7 Jun 2022 18:37:16 GMT"