I'm using Phoenix and Ecto. I need to check if a difference between "now()" and a certain date less than 3 days. Exactly I'm trying this:
{diff_days, {_, _, _}} = Ecto.DateTime.to_erl(my_user.signed_up_at) |> :calendar.time_difference(:calendar.universal_time)
if diff_days <= 3 do
# good to go
# ....
end
However, this won't for a date older than 3 days and 1 minute, right?
What's the univeral way to check if it's not more but 3 days or 72 hours?
In my database signed_up_at
is of type timestamp without timezone
Use DateTime.diff/3
:
three_days_in_seconds = 3 * 24 * 60 * 60
case DateTime.diff(DateTime.utc_now, my_user.signed_up_at, :second) do
good when good < three_days_in_seconds ->
# good to go
_ ->
# skip
end
Please note, that :second
is a default value for the third argument in a call to DateTime.diff/3
and might be omitted.