TIL: Correctly comparing dates in Elixir

Comparing objects with each other is always a threat. A prime example of this are dates that you should never compare by math operations. Special functions are used to compare them, thanks to which a comparison based on semantics is possible.

It can be particularly troublesome when you are just starting your adventure with Elixir. You’ll get used to it after a while. However, you have to be careful at first.

Compare the dates

If we want to compare two dates, we should never do something like this:

1
2
  iex(1)> ~D[2022-03-14] > ~D[2022-01-20]
  false

As a result, we may get erroneous results, which is also the case here. Fortunately, we receive appropriate information about the problem:

1
2
3
4
  warning: invalid comparison with struct literal %Date{year: 2022, month: 3, day: 14}.
  Comparison operators (>, <, >=, <=, min, and max) perform structural and not semantic comparison.
  Comparing with a struct literal is unlikely to give a meaningful result.
  Struct modules typically define a compare/2 function that can be used for semantic comparison

The documentation can specify more about the Structural comparison. In short: instead of comparing structures based on the importance of individual fields, they were compared field-by-field without any semantics.

Use Date.compare/2 and DateTime.compare/2 in your code to avoid date comparison problems.

Get new posts and extra comments

You'll receive every new post with extra unpublished comments available only to the subscribers!

I won't send you spam. Unsubscribe at any time.