Comparing dates is a common procedure in day to day jobs. Whether you need them to trigger future, or past jobs you will need to know how to do it in Elixir.
To acomplish this task, you will use the
.compare/2 method. Which exists in both
Upon success compare, the method will return any of the next atoms
:lt | :eq | :gt.
DateTime utc format comparison where the first element is greater than the second.
iex(5)> DateTime.compare(~U[2022-04-10 04:51:27.626455Z], ~U[2022-03-10 04:51:27.626455Z]) :gt
Date comparison where the first element is lesser than the second.
iex(2)> Date.compare(~D[2022-04-01], ~D[2022-04-08]) :lt
After using other tools in differents environments like
date-fns, here you get exactly the result of the comparison.