自从 Ecto.DateTime 被弃用后,您如何创建新的 DateTime?
How do you create a new DateTime since Ecto.DateTime was deprecated?
我目前有一个 Date
和一个 Time
,我真的很想将它们组合成一个 DateTime
结构。
在 Ecto 3 之前,您可以使用 Ecto.DateTime.from_date_and_time
执行此操作,但在新文档中,由于不推荐使用 Ecto 类型,我找不到等效的函数。
函数目前看起来像:
def add_datetime(date_as_string) do
(_, date = Date.from_iso8601(date)
end_time = #T[23:59:59]
datetime = datetime_add(Ecto.DateTime.from_date_and_time(date, end_time), -3, "day")
end
这个特定项目的一个限制是我想尽可能避免添加像 Timex 这样的第三方库,但在查看当前的 Elixir 文档后我迷路了。
您可以使用 DateTime.from_iso8601/2
.
datetime_iso8601 = "#{Date.to_iso8601(date)}T#{Time.to_iso8601(time)}+03:30"
{:ok, datetime, offset_from_utc} = DateTime.from_iso8601(datetime_iso8601)
而不是 +3:30
使用您想要的偏移量,或者 Z
用于 UTC。
对于在 Google 上找到此内容的任何人,如果您不关心时区信息,也可以使用 NaiveDateTime
:
datetime= NaiveDateTime.new(date_struct, time_struct)
|> DateTime.from_naive("Etc/UTC")
我目前有一个 Date
和一个 Time
,我真的很想将它们组合成一个 DateTime
结构。
在 Ecto 3 之前,您可以使用 Ecto.DateTime.from_date_and_time
执行此操作,但在新文档中,由于不推荐使用 Ecto 类型,我找不到等效的函数。
函数目前看起来像:
def add_datetime(date_as_string) do
(_, date = Date.from_iso8601(date)
end_time = #T[23:59:59]
datetime = datetime_add(Ecto.DateTime.from_date_and_time(date, end_time), -3, "day")
end
这个特定项目的一个限制是我想尽可能避免添加像 Timex 这样的第三方库,但在查看当前的 Elixir 文档后我迷路了。
您可以使用 DateTime.from_iso8601/2
.
datetime_iso8601 = "#{Date.to_iso8601(date)}T#{Time.to_iso8601(time)}+03:30"
{:ok, datetime, offset_from_utc} = DateTime.from_iso8601(datetime_iso8601)
而不是 +3:30
使用您想要的偏移量,或者 Z
用于 UTC。
对于在 Google 上找到此内容的任何人,如果您不关心时区信息,也可以使用 NaiveDateTime
:
datetime= NaiveDateTime.new(date_struct, time_struct)
|> DateTime.from_naive("Etc/UTC")