ClojureScript date-time library
Asked Answered
R

5

11

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.

Rockbottom answered 11/6, 2013 at 9:45 Comment(0)
R
11

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}
Rete answered 10/7, 2013 at 14:50 Comment(1)
Be aware that Moment uses mutable dates.Anaphylaxis
J
17

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

Juneberry answered 23/12, 2014 at 18:11 Comment(0)
R
11

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}
Rete answered 10/7, 2013 at 14:50 Comment(1)
Be aware that Moment uses mutable dates.Anaphylaxis
H
1

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

Halmstad answered 9/7, 2013 at 9:18 Comment(0)
Y
1

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
Yevetteyew answered 21/8, 2019 at 4:51 Comment(0)
H
0

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.

Herzegovina answered 20/1, 2021 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.