Skip to content

Instantly share code, notes, and snippets.

@oakmac
Last active Aug 13, 2021
Embed
What would you like to do?
find random date between two ranges in Clojure
;; question: i want to generate a random date between 2018 and 2020. how can i do it in clojure?
(require [java-time :as jt])
(defn date-range
"returns a sequences of dates between start and end (exclusive)"
[start-date end-date]
(let [dates-from-start (jt/iterate jt/plus start-date (jt/days 1))]
(take-while #(jt/before? % end-date) dates-from-start)))
;; returns a random date between 2018 and 2020
(rand-nth (date-range (jt/local-date 2018 1 1) (jt/local-date 2020 1 1)))
;; this code licensed as CC0 by Chris Oakman <chris@oakmac.com>, Aug 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment