Adam Schmideg
Adam Schmideg

Reputation: 10850

ClojureScript date-time library

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.

Upvotes: 11

Views: 5257

Answers (5)

avocade
avocade

Reputation: 1323

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.

Upvotes: 0

Jeremy Field
Jeremy Field

Reputation: 692

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

Upvotes: 1

Ruslan Prakapchuk
Ruslan Prakapchuk

Reputation: 614

Too late, but for those who come by search, there is cljs-time library.

Upvotes: 17

jbouwman
jbouwman

Reputation: 2117

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}

Upvotes: 11

hequ
hequ

Reputation: 771

The project I'm working at the moment uses moment.js. It works pretty much ok with clojurescript. I recommend to check that out.

Upvotes: 1

Related Questions