【发布时间】:2016-06-08 21:42:56
【问题描述】:
假设我有两个日期(A 和 B)由
检索:calendar.universal_time(), e.g. {{2016, 5, 5}, {13, 17, 29}}
我需要确定日期 A 是否比日期 B 大 30 天(或更长时间)。
如何在 Elixir 中做到这一点?
【问题讨论】:
假设我有两个日期(A 和 B)由
检索:calendar.universal_time(), e.g. {{2016, 5, 5}, {13, 17, 29}}
我需要确定日期 A 是否比日期 B 大 30 天(或更长时间)。
如何在 Elixir 中做到这一点?
【问题讨论】:
您可以使用:calendar.time_to_gregorian_days。
iex(1)> {date, time} = {{2016, 5, 5}, {13, 17, 29}}
{{2016, 5, 5}, {13, 17, 29}}
iex(2)> :calendar.date_to_gregorian_days(date)
736454
对两个日期执行此操作并减去值并检查该值是否大于 30。
【讨论】:
date_to_gregorian_seconds。
您可以在两个日期时间上调用:calendar.datetime_to_gregorian_seconds/1,减去,并将结果与30 * 24 * 60 * 60(30 天的秒数)进行比较:
defmodule A do
def thirty_days_apart(low, high) do
min = 30 * 24 * 60 * 60
(:calendar.datetime_to_gregorian_seconds(high) -
:calendar.datetime_to_gregorian_seconds(low)) >= min
end
end
IO.inspect A.thirty_days_apart({{2000, 1, 1}, {0, 0, 0}}, {{2000, 2, 1}, {0, 0, 0}})
IO.inspect A.thirty_days_apart({{2000, 2, 1}, {0, 0, 0}}, {{2000, 3, 1}, {0, 0, 0}})
打印
true
false
【讨论】:
你可以使用Timex:
a = :calendar.universal_time() |> Timex.DateTime.from_erl
b = {{2016, 4, 5}, {13, 17, 29}} |> Timex.DateTime.from_erl
if Timex.Comparable.diff( a, b, :days ) > 30 do
# ...
end
【讨论】: