Skip to content

Instantly share code, notes, and snippets.

@wunki
Created December 17, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wunki/3b63ca34e882a9584491 to your computer and use it in GitHub Desktop.
Save wunki/3b63ca34e882a9584491 to your computer and use it in GitHub Desktop.
Get the last weeks in Clojure.
(defn get-weeks [n]
"Returns a vector of dictionaries containing the start and end days of the last n weeks"
(let [now (t/today-at-midnight)
this-day-num (Integer. (f/unparse (f/formatter "e") now))
last-monday (t/minus now (t/days (dec this-day-num)))
span-fn (fn [v w]
(let [monday (t/minus last-monday (t/weeks w))
sunday (t/plus monday (t/days 7))
week (Integer. (f/unparse (f/formatter "w") monday))]
(conj v {:week week
:monday monday
:sunday sunday})))]
(reduce span-fn [] (range n))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment