如何在elixir中以ISO 8601格式生成当前日期? [英] How to generate current date in ISO 8601 format in elixir?

查看:82
本文介绍了如何在elixir中以ISO 8601格式生成当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以与以下相同的格式生成当前时间的字符串: 20130524T000000Z

I need to generate a string with the current time in the same format as the following: 20130524T000000Z

该示例是 Fri的时间戳记,2013年5月24日格林尼治标准时间$

我该怎么办那?有没有外部软件包的方法?

How would I do that? Is there a way to do it without external packages?

推荐答案

2016年7月18日更新

Elixir 1.3 及更高版本对此提供了本地支持:

Elixir 1.3 and up has support for this natively:

iex> DateTime.utc_now() |> DateTime.to_iso8601()
"2016-07-18T21:49:08.132428Z"

原始版本2015-12-01 (已在2016-07-18中添加了版本差异,谢谢@sebastian_k)

如果您不介意使用外部库,则可以使用出色的 timex 库:

You could use the excellent timex library if you didn't mind using an external library:

timex 1.x docs

iex> Timex.Date.local |> Timex.DateFormat.format("{ISOz}")
{:ok, "2015-12-01T09:40:44.716Z"}

timex 2.x 文档

iex> Timex.DateTime.local() |> Timex.format("{ISOz}")
{:ok, "2015-12-01T09:40:44.716Z"}

timex 3.x 文档

iex> Timex.now() |> Timex.format("{ISO:Extended:Z}")
{:ok, "2015-12-01T09:40:44.716417ZZ"}

这篇关于如何在elixir中以ISO 8601格式生成当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆