DateTime.now 在 Elixir 和 Ecto 中
DateTime.now in Elixir and Ecto
我想在没有第三方库的情况下获取 Phoenix/Elixir 中的当前日期时间戳。或者简单地说,我想要 DateTime.now()
这样的东西。我该怎么做?
Ecto Ecto.DateTime.utc/1
获取当前 UTC 时间:
iex(1)> Ecto.DateTime.utc
#Ecto.DateTime<2016-09-05 13:30:04>
iex(2)> Ecto.DateTime.utc(:usec) # include microseconds
#Ecto.DateTime<2016-09-05 13:30:18.367318>
如果你想要本地系统时区的当前时间,你可以这样做:
Ecto.DateTime.from_erl(:erlang.localtime)
从 Ecto 3 开始,Ecto.Date
、Ecto.Time
和 Ecto.DateTime
不再存在,如 here.
所述
但是,Elixir 现在附带 DateTime, Date and NaiveDateTime,应该使用它。
iex(1)> DateTime.utc_now
#DateTime<2019-01-14 12:05:52.271492Z>
我想在没有第三方库的情况下获取 Phoenix/Elixir 中的当前日期时间戳。或者简单地说,我想要 DateTime.now()
这样的东西。我该怎么做?
Ecto Ecto.DateTime.utc/1
获取当前 UTC 时间:
iex(1)> Ecto.DateTime.utc
#Ecto.DateTime<2016-09-05 13:30:04>
iex(2)> Ecto.DateTime.utc(:usec) # include microseconds
#Ecto.DateTime<2016-09-05 13:30:18.367318>
如果你想要本地系统时区的当前时间,你可以这样做:
Ecto.DateTime.from_erl(:erlang.localtime)
从 Ecto 3 开始,Ecto.Date
、Ecto.Time
和 Ecto.DateTime
不再存在,如 here.
但是,Elixir 现在附带 DateTime, Date and NaiveDateTime,应该使用它。
iex(1)> DateTime.utc_now
#DateTime<2019-01-14 12:05:52.271492Z>