Thinking Elixir 159: Langchain with Elixir and Safe Ecto Migrations IntelliJ Elixir v15.1.0

Comparing Datetimes and Dates in Elixir

Elixir 1.15 introduced new DateTime and Date functions to compare dates.

Let’s cover the new functions, what already exists, and what to avoid at all costs!

  • New ✨: DateTime.before?/2, DateTime.after?/2 (and their Date counterparts) are new (since Elixir 1.15) and allow for strict comparison of datetimes (and dates),
  • We’ve had DateTime.compare/2 for a while, and it returns :gt, :lt, or :eq. The nice thing about compare/2 is that it has equality.
  • Never use <, >, == to compare dates or datetimes! They perform structural comparisons. They don’t understand semantics.

Check out a short video of me walking through those! 👇 https://www.elixirstreams.com/tips/comparing-elixir-datetimes-and-dates