Skip to content

Instantly share code, notes, and snippets.

@nivekuil
Created October 14, 2023 18:59
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 nivekuil/f3698345ac51f6adf8039b475288461a to your computer and use it in GitHub Desktop.
Save nivekuil/f3698345ac51f6adf8039b475288461a to your computer and use it in GitHub Desktop.
clojurescript relative time
#?(:cljs (def relative-time-formatter
(js/Intl.RelativeTimeFormat. "en" #js{:numeric "auto"})))
(defn timeago [time]
#?(:clj (str time)
:cljs (let [seconds-ago (t/seconds (t/between (t/now) time))
[divide unit] (condp #(> %1 (abs %2)) seconds-ago
60 [1 "second"]
3600 [60 "minute"]
86400 [3600 "hour"]
[86460 "day"])]
(.format relative-time-formatter (quot seconds-ago divide) unit))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment