I'd like to do some basic, but not very basic date-related operations on ClojureScript, like getting the days between two dates. There is clj-time which is a wrapper around Joda time, so it's Clojure only. I'm also aware of the date classes in Google Closure Library. There are many possibilites for JavaScript, see https://stackoverflow.com/questions/802861/javascript-date-manipulation-library or https://stackoverflow.com/questions/996995/javascript-date-time-library-recommendations. I wonder if there is an idiomatic ClojureScript way for this. If there is no such beast, I wonder which JavaScript library would be the best candidate for wrapping.
http://momentjs.com is easy to use for date arithmetic.
For example, the difference between two dates, in number of days:
(defn mom []
(let [log (fn [& args] (.log js/console (apply str args)))
days-ago (fn [n] (.subtract (js/moment) "days" n))]
(log {:difference (.diff (days-ago 7) (days-ago 28) "days")})))
(mom) ==> {:difference 21}
Too late, but for those who come by search, there is cljs-time library.
http://momentjs.com is easy to use for date arithmetic.
For example, the difference between two dates, in number of days:
(defn mom []
(let [log (fn [& args] (.log js/console (apply str args)))
days-ago (fn [n] (.subtract (js/moment) "days" n))]
(log {:difference (.diff (days-ago 7) (days-ago 28) "days")})))
(mom) ==> {:difference 21}
The project I'm working at the moment uses moment.js. It works pretty much ok with clojurescript. I recommend to check that out.
If you want something cross-platform, try juxt/tick
Regarding days between two dates, this seems to work on both platforms (calling into the underlying libraries for .until
):
(require '[tick.alpha.api :as t])
(require '[tick.core])
(.until (t/new-date 2019 1 1) (t/new-date 2019 3 5) (tick.core/unit-map :days))
;=> 63
For date picking, the react-select project has an experimental date picker with fuzzy date support. We're using the regular react-select
component wrapped in cljs, seems to work pretty well.
© 2022 - 2024 — McMap. All rights reserved.