A date and time library for ClojureScript, imitating the API of the
Cljs-time is an attempt at replicating the functionality and API of
clj-time. This is not a drop-in clojurescript replacement for
clj-time, however the goal is that over time enough functionality of
the clj-time API can be replicated to make this library "good enough"
for other projects.
This library is currently leaning on the
Google Closure goog.date
library for basic date/time functionality. The date objects in this
library are mutable, however any operations that alter a date
object return a copy, leaving the referenced date object alone. In the
future, immutable date objects will be preferred.
cljs-time artifacts are released to Clojars.
If you are using Maven, add the following repository definition to your
Please open issues against the
cljs-time repo on Github.
Note: version 0.5. follows the API of clj-time 0.11.0.*
Most of the clj-time API has been implemented. Some of the parts that
don't make sense in clojurescript, such as Java interop, and
minimal, and therefore so is cljs-time's.
The majority of the tests from clj-time have been ported, and are
API documentation is
dates) is not the same as in Joda/clj-time. Two date objects
representing the same instant in time in goog.date.* are not equal.
If you need to test for equality use either
optionally you can require the
cljs-time.extend namespace which will
extend the goog.date.* datatypes, so that clojure.core/= works as
If you want your goog.date.* serializable with
The main namespace for date-time operations in the
=> (use 'cljs-time.core)
Create a DateTime instance with date-time, specifying the year, month,
day, hour, minute, second, and millisecond:
=> (date-time 1986 10 14 4 3 27 456)
Less-significant fields can be omitted:
=> (date-time 1986 10 14)
Get the current time with
(now) and the start of the Unix epoch with
Once you have a date-time, use accessors like
second to access the corresponding fields:
=> (hour (date-time 1986 10 14 22))
The date-time constructor always returns times in the UTC time zone.
If you only want a date with no time component, consider using the
LocalDate instances that do not have time components (and thus don't suffer from timezone-related shifting).
=> (local-date 2013 3 20)
before? determine the relative position of two
=> (after? (date-time 1986 10) (date-time 1986 9))
Often you will want to find a date some amount of time from a given date. For
example, to find the time 1 month and 3 weeks from a given date-time:
=> (plus (date-time 1986 10 14) (months 1) (weeks 3))
Interval is used to represent the span of time between two
instances. Construct one using
interval, then query them using
=> (within? (interval (date-time 1986) (date-time 1990))
in-minutes functions can be used to describe intervals in the corresponding temporal units:
=> (in-minutes (interval (date-time 1986 10 2) (date-time 1986 10 14)))
If you need to parse or print date-times, use
=> (use 'cljs-time.format)
Printing and parsing are controlled by formatters. You can either use one of the built in ISO8601 formatters or define your own, e.g.:
(def built-in-formatter (formatters :basic-date-time))
(def custom-formatter (formatter "yyyyMMdd"))
To see a list of available built-in formatters and an example of a date-time printed in their format:
mm is minutes,
MM is months,
ss is seconds and
SS is milliseconds.
Once you have a formatter, parsing and printing are straight-forward:
=> (parse custom-formatter "20100311")
=> (unparse custom-formatter (date-time 2010 10 3))
cljs-time.core/today-at returns a moment in time at the given hour, minute and second
on the current date:
=> (today-at 12 00)
=> (today-at 12 00 05)
cljs-time.coerce contains utility functions for coercing Google Closure
DateTime instances to and from various other types:
=> (use 'cljs-time.coerce)
For example, to convert a goog.date
DateTime to and from a js
=> (to-long (date-time 1998 4 25))
=> (from-long 893462400000)
And by the magic of protocols you can pass in an isoformat string and get the unix epoch milliseconds:
=> (to-long "2013-08-01")
Running the tests:
$ boot test-all
$ boot auto-test
$ boot repl # (or jack-in from cider/etc)
clojurescript node.js repl server listening on 56950
to quit, type: :cljs/quit
cljs.user> (+ 1 1)
The complete API documentation
is also available.
Copyright © 2013-2016 Andrew Mcveigh
Distributed under the Eclipse Public License, the same as Clojure.