Last active
August 13, 2021 06:33
-
-
Save oakmac/00c82f4be7f4eca9dbb13cf0497307b6 to your computer and use it in GitHub Desktop.
find random date between two ranges in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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