Calculating a date in the future or past

Calculating a date in the future or past

How to:

In Clojure, you’ll mainly use the clj-time library for date operations. Here’s a quick show:

(require '[clj-time.core :as time])
(require '[clj-time.coerce :as coerce])
(require '[clj-time.periodic :as periodic])

;; Add 5 days to the current date
(let [now (time/now)
      five-days (time/plus now (time/days 5))]
  (str "Five days from now: " (coerce/to-string five-days)))

;; Subtract 10 days from a specific date
(let [specific-date (coerce/to-date-time "2023-03-01T12:00:00.000Z")
      ten-days-ago (time/minus specific-date (time/days 10))]
  (str "Ten days before March 1, 2023: " (coerce/to-string ten-days-ago)))

Sample output:

"Five days from now: 2023-03-23T08:00:00.000Z"
"Ten days before March 1, 2023: 2023-02-19T12:00:00.000Z"

Deep Dive

In the earlier days, Coders used Java’s Date and Calendar classes. But, let’s be honest, they’re a headache—verbose and error-prone. The clj-time library brought some sanity, wrapping Joda-Time’s more developer-friendly API.

Alternatives? Java 8 introduced java.time (JSR-310), which is quite good, but in Clojure’s neck of the woods, we’re still cozy with clj-time.

When calculating dates, you use periods for concepts like “days” and “months” and durations for precise millisecond counts. Keep in mind time zones—dates and times can shift dramatically depending on the time zone rules, and daylight saving time (DST) can throw a spanner in your works.

See Also