在 Elixir 中,如何格式化相对于现在的日期时间?

ijt*_*ijt 0 elixir

我记得找到了一些可以让您将 DateTime 转换为“in 1 hour”之类的字符串的东西,但到目前为止我的搜索结果是空的。

Kip*_*Kip 5

ex_cldr_dates_times还具有功能,包括本地化。

\n
iex> Cldr.DateTime.Relative.to_string(~U[2017-04-29 09:00:00Z], relative_to: ~U[2017-04-29 08:00:00Z])            \n{:ok, "in 1 hour"}\niex> Cldr.DateTime.Relative.to_string(~U[2017-04-29 09:00:00Z], relative_to: ~U[2017-04-29 08:00:00Z], locale: "de")\n{:ok, "in 1 Stunde"}\niex> Cldr.DateTime.Relative.to_string(~U[2017-04-29 09:00:00Z], relative_to: ~U[2017-04-29 08:00:00Z], locale: "ja")\n{:ok, "1 \xe6\x99\x82\xe9\x96\x93\xe5\xbe\x8c"}\niex> Cldr.DateTime.Relative.to_string(~U[2017-04-29 09:00:00Z], relative_to: ~U[2017-04-29 11:05:00Z])              \n{:ok, "2 hours ago"}\n
Run Code Online (Sandbox Code Playgroud)\n