【问题标题】:DateTime.now in Elixir and EctoElixir 和 Ecto 中的 DateTime.now
【发布时间】:2017-01-12 21:20:42
【问题描述】:

我想在没有第三方库的情况下获取 Phoenix/Elixir 中的当前日期时间戳。或者简单地说,我想要DateTime.now() 之类的东西。我该怎么做?

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

    从 Ecto 3 开始,Ecto.DateEcto.TimeEcto.DateTime 不再存在,如 here. 所述

    不过,Elixir 现在附带 DateTimeDateNaiveDateTime,应该使用它们。

    iex(1)> DateTime.utc_now
    #DateTime<2019-01-14 12:05:52.271492Z>
    

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多