Raddi_On
How to get a difference between "now()" and another date and make sure it doesn't exceed N days?
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
Most Liked
gon782
I flagged your comment because you were using very inflammatory language without any provocation or reason, precisely as you are doing in this one. Please try to be less vitriolic in your language on the forum. There is no reason to be using terms like “idiot” or saying people would be mentally deficient or whatever if they do things differently than you.
crabonature
If you want use just :calendar without libraries, you can just do it simpy this way:
defmodule DateHelperTest do
use ExUnit.Case
test "date is not older than 3 days" do
assert DateHelper.not_older_than?({{2018, 1, 9}, {}}, 3)
assert DateHelper.not_older_than?({{2018, 1, 15}, {}}, 3)
refute DateHelper.not_older_than?({{2016, 1, 9}, {}}, 3)
refute DateHelper.not_older_than?({{2018, 1, 7}, {}}, 3)
refute DateHelper.not_older_than?({{2018, 1, 8}, {}}, 3)
end
end
defmodule DateHelper do
def not_older_than?(date, days) do
diff_days(extract_date(:calendar.universal_time), extract_date(date)) <= days
end
defp extract_date({date, _}), do: date
defp diff_days(d1, d2) do
:calendar.date_to_gregorian_days(d1) - :calendar.date_to_gregorian_days(d2)
end
end
# and your code:
if DateHelper.not_older_than?(Ecto.DateTime.to_erl(my_user.signed_up_at), 3) do
# good to go
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









