如何使用Ecto获取明天的约会时间?

时间:2017-07-20 09:58:19

标签: elixir ecto

我可以像这样使用Ecto获取当前日期时间:

Ecto.DateTime.utc

但是我如何让明天像

一样
Ecto.DateTime.utc + timedelta 1 day

4 个答案:

答案 0 :(得分:4)

如何解答这个问题?

Ecto.DateTime.utc 
|> Ecto.DateTime.to_erl 
|> :calendar.datetime_to_gregorian_seconds 
|> Kernel.+(86400) 
|> :calendar.gregorian_seconds_to_datetime 
|> Ecto.DateTime.from_erl

答案 1 :(得分:2)

Timex.shift(Timex.now, days: 1)

https://github.com/bitwalker/timex

答案 2 :(得分:1)

Ecto.DateTime是一个包含以下字段的结构:

defstruct [:year, :month, :day, :hour, :min, :sec, usec: 0]

如果你想加1天,你应该可以这样做:

newDate = %{oldDate | day: oldDate.day + 1}

//修改

我已经在IEx中进行了测试,它按预期工作:

iex(1)> oldDate = Ecto.DateTime.utc
#Ecto.DateTime<2017-07-21 21:17:06>
iex(2)> newDate = newDate = %{oldDate | day: oldDate.day + 1}
#Ecto.DateTime<2017-07-22 21:17:06>

答案 3 :(得分:0)

iex(3)> t = DateTime.utc_now()
#DateTime<2018-07-17 08:22:11.472192Z>
iex(4)> n = %{t | day: t.day + 1}
#DateTime<2018-07-18 08:22:11.472192Z>
iex(5)> n
#DateTime<2018-07-18 08:22:11.472192Z>
iex(6)> m = %{t | day: t.day - 1}
#DateTime<2018-07-16 08:22:11.472192Z>
iex(7)> m
#DateTime<2018-07-16 08:22:11.472192Z>
相关问题