Created
October 14, 2023 18:59
-
-
Save nivekuil/f3698345ac51f6adf8039b475288461a to your computer and use it in GitHub Desktop.
clojurescript relative time
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
#?(: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