简体   繁体   English

使用 Elixir 格式化日期

[英]Format date with Elixir

I'm trying to format the Timex module to look a certain way.我正在尝试将 Timex 模块格式化为某种方式。 I'm trying to get today's date.我想知道今天的日期。 but I want it formatted like this:但我希望它的格式如下:

2017/12/12. 2017 年 12 月 12 日。

year/mn/day年/月/日

In ruby I would go to the strftime class but I'm not sure how to do this with Elixir:在 ruby​​ 中,我会去 strftime 课程,但我不确定如何使用 Elixir 来做到这一点:

Current attempt:当前尝试:

Timex.local => #DateTime<2017-12-12 19:57:17.232916-05:00 EST America/Detroit>

How can I take that and format it how I specified?我怎样才能接受它并按照我指定的方式格式化它?

Timex is a third-party library that was created in the era when Elixir had no good support for dates/times. Timex是在 Elixir 对日期/时间没有很好支持的时代创建的第三方库。 Nowadays, there is DateTime native class in the core, so I am unsure why do you want to use Timex at all.现在,核心中有DateTime原生类,所以我不确定你为什么要使用Timex

In any case, DateTime is a struct:无论如何, DateTime是一个结构体:

iex|1 ▶ today = DateTime.utc_now
#⇒ #DateTime<2017-12-13 07:22:58.290075Z>
iex|2 ▶ [today.year, today.month, today.day]
#⇒ [2017, 12, 13]
iex|3 ▶ Enum.join [today.year, today.month, today.day], "/"
#⇒ "2017/12/13"

To pad with leading zeroes for "2018/1/1":为“2018/1/1”填充前导零:

iex|4 ▶ with {:ok, today} <- Date.new(2018, 1, 1) do
...|4 ▶   [today.year, today.month, today.day]
...|4 ▶   |> Enum.map(&to_string/1)
...|4 ▶   |> Enum.map(&String.pad_leading(&1, 2, "0"))
...|4 ▶   |> Enum.join("/")
...|4 ▶ end
#⇒ "2018/01/01"

If you want to do this without an external library, you can use io_lib:format/2 to pad the integers with zeroes where necessary like this:如果你想在没有外部库的情况下做到这一点,你可以使用io_lib:format/2在必要时用零填充整数,如下所示:

iex(1)> date = Date.utc_today
~D[2017-12-13]
iex(2)> :io_lib.format("~4..0B/~2..0B/~2..0B", [date.year, date.month, date.day]) |> IO.iodata_to_binary
"2017/12/13"
iex(3)> {:ok, date} = Date.new(2018, 1, 1)
{:ok, ~D[2018-01-01]}
iex(4)> :io_lib.format("~4..0B/~2..0B/~2..0B", [date.year, date.month, date.day]) |> IO.iodata_to_binary
"2018/01/01"
iex(5)> {:ok, date} = Date.new(1, 1, 1)
{:ok, ~D[0001-01-01]}
iex(6)> :io_lib.format("~4..0B/~2..0B/~2..0B", [date.year, date.month, date.day]) |> IO.iodata_to_binary
"0001/01/01"

你可以这样做来添加零

Timex.local |>  Timex.format!("{YYYY}/0{M}/0{D}") => "2017/01/01"

Elixir 1.11 has Calendar.strftime/3 built-in for your strftime needs. Elixir 1.11 内置了Calendar.strftime/3以满足您的strftime需求。

Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%y-%m-%d %I:%M:%S %p")
"19-08-26 01:52:06 PM"

So it appears the Timex Module has a format!/2 function which will return a string of what ever date you pass to it.所以看起来 Timex 模块有一个format!/2函数,它将返回你传递给它的任何日期的字符串。

Here is what I came up with: Timex.local |> Timex.format!("{YYYY}/{M}/{D}") => "2017/12/12"这是我想出的: Timex.local |> Timex.format!("{YYYY}/{M}/{D}") => "2017/12/12"

The answer to add 0 padding is incorrect and will always pad 0s even if it does not require it.添加 0 填充的答案是不正确的,即使不需要它也会始终填充 0。 The correct way to pad 0s is as follows:填充0的正确方法如下:

Timex.local |>  Timex.format!("{YYYY}/{0M}/{0D}") => "2017/01/01"

As of Elixir 1.11 you can do this sanely with a date formatter in the standard library's Calendar module:从 Elixir 1.11 开始,您可以使用标准库Calendar模块中的日期格式化程序来完成此操作:

https://hexdocs.pm/elixir/1.13.0/Calendar.html#strftime/3 https://hexdocs.pm/elixir/1.13.0/Calendar.html#strftime/3

DateTime.utc_now()
|> Calendar.strftime("%Y/%m/%d")

"2022/01/12" “2022/01/12”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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