简体   繁体   中英

Format date with Elixir DateTime to RFC2616 format

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.

Elixir 1.11.0 introducedCalendar.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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM