格式日期与elixir dateTime到RFC2616格式

发布于 2025-02-05 21:29:00 字数 223 浏览 3 评论 0原文

我想实现一个代码,将DateTime对象转换为〜U [2022-06-07 18:37:16.842920Z] ,例如TUE,2022年6月7日, 37:16 GMT。我不想使用calendar.datetime.format.httpdate,因为我们的代码库已经使用了DateTime。

我需要它以RFC2616格式发送API标头。任何帮助都将受到赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2025-02-12 21:29:00

Elixir 1.11.0引入 /a>这就是您所需要的。请注意,DateTime日历是有不同目的的内置模块。使用它们的混合应该没有问题。

~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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文