Skip to content

Instantly share code, notes, and snippets.

@oakmac
Last active August 13, 2021 06:33
Show Gist options
  • Save oakmac/00c82f4be7f4eca9dbb13cf0497307b6 to your computer and use it in GitHub Desktop.
Save oakmac/00c82f4be7f4eca9dbb13cf0497307b6 to your computer and use it in GitHub Desktop.
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 <[email protected]>, Aug 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment