Raddi_On

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

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

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

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement