#inst literal

tagged literalsince v0.0-1424 in clojure in ednEdit

Creates a JavaScript Date object using RFC-3339 formatted string.

  • #inst "yyyy-mm-dd" - date
  • #inst "yyyy-mm-ddThh:mm:ss" - date and time
  • #inst "yyyy-mm-ddThh:mm:ssZ" - specify UTC
  • #inst "yyyy-mm-ddThh:mm:ss-hh:mm" - specify negative time zone offset
  • #inst "yyyy-mm-ddThh:mm:ss+hh:mm" - specify positive time zone offset


#inst "1985-04-12"
;;=> #inst "1985-04-12T00:00:00.000-00:00"

#inst "1985-04-12T23:20:50.52Z"
;;=> #inst "1985-04-12T23:20:50.520-00:00"

Dates can be tested for order, as native JavaScript Dates can:

(def a #inst "2005-01-20")
(def b #inst "2005-01-21")

(< a b)
;;=> true

Dates can be tested for equality, unlike JavaScript Dates. (ClojureScript extends js/Date type with IEquiv protocol to allow this.)

(def a #inst "2012-06-13")
(def b #inst "2012-06-13")

(= a b)
;;=> true

See Also:

Reader code @ clojurescript:src/main/clojure/cljs/tagged_literals.cljc
   (defn read-inst
     (when-not (string? form)
       (throw (RuntimeException. "Instance literal expects a string for its timestamp.")))
       (inst/read-instant-instant form)
       (catch Throwable e
         (throw (RuntimeException. (.getMessage e))))))

Reader table @ clojurescript:src/main/clojure/cljs/tagged_literals.cljc
(def ^:dynamic *cljs-data-readers*
  (merge ;; assumes we can read all data_readers
    #?(:clj *data-readers*)
    {'queue read-queue
     'uuid  read-uuid
     'inst  read-inst
     'js    read-js}))