Skip to content

Instantly share code, notes, and snippets.

@remvee
Last active June 25, 2022 00:34
Show Gist options
  • Save remvee/2735ee151ab6ec075255 to your computer and use it in GitHub Desktop.
Save remvee/2735ee151ab6ec075255 to your computer and use it in GitHub Desktop.
Calculate week number in clojurescript
(defn week-number
"Week number according to the ISO-8601 standard, weeks starting on
Monday. The first week of the year is the week that contains that
year's first Thursday (='First 4-day week'). The highest week number
in a year is either 52 or 53."
[ts]
(let [year (.getFullYear ts)
month (.getMonth ts)
date (.getDate ts)
day (.getDay ts)
thursday (js/Date. year month (- (+ date 4) (if (= 0 day) 7 day)))
year-start (js/Date. year 0 1)]
(Math/ceil (/ (+ (/ (- (.getTime thursday)
(.getTime year-start))
(* 1000 60 60 24))
1)
7))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment