cljs-time

repository·master·Indexed 43 days ago

https://github.com/andrewmcveigh/cljs-time

A ClojureScript library for date and time manipulation that imitates the clj-time API. Built on top of the Google Closure goog.date library, it provides namespaces for core operations, formatting, and type coercion.

Tokens
1.2K
Snippets
4
Records
5
Agent score
6%

What's inside cljs-time

  1. How equality works in cljs-time

    master

    Standard equality in Google Closure goog.date.* (and plain JavaScript Date objects) does not work the same way as in Joda/clj-time. Two date objects representing the same instant are not considered equal by default.

    To perform equality checks, you have two options:

    1. Use cljs-time.core/=.
    2. Require the cljs-time.extend namespace, which extends goog.date.* datatypes so that standard clojure.core/= works as expected.
  2. Install cljs-time

    master

    To use cljs-time in your project, add the following dependency to your Leiningen configuration. Note that version 0.5.* follows the API of clj-time 0.11.0.

    If you are using Maven, you must first add the Clojars repository to your pom.xml:

    <repository>
      <id>clojars.org</id>
      <url>http://clojars.org/repo</url>
    </repository>
    ;; Leiningen dependency
    [com.andrewmcveigh/cljs-time "0.5.2"]
  3. Coerce types with cljs-time.coerce

    master

    The cljs-time.coerce namespace provides utilities to convert Google Closure DateTime instances to and from other types, such as JavaScript numbers (milliseconds) or ISO format strings.

    (use 'cljs-time.coerce)
    
    ;; Convert DateTime to Unix epoch milliseconds (Long)
    (to-long (date-time 1998 4 25))
    
    ;; Convert Unix epoch milliseconds to DateTime
    (from-long 893462400000)
    
    ;; Convert an ISO format string to Unix epoch milliseconds
    (to-long "2013-08-01")
  4. Parse and print dates with cljs-time.format

    master

    Use cljs-time.format to convert date-time objects to strings (unparse) or strings to date-time objects (parse). Formatting is handled by formatter objects.

    Formatter Syntax Notes:

    • mm: minutes
    • MM: months
    • ss: seconds
    • SSS: milliseconds

    Use show-formatters to see a list of available built-in formatters.

    (use 'cljs-time.format)
    
    ;; Define formatters
    (def built-in-formatter (formatters :basic-date-time))
    (def custom-formatter (formatter "yyyyMMdd"))
    
    ;; Parsing a string
    (parse custom-formatter "20100311")
    
    ;; Unparsing a date-time to a string
    (unparse custom-formatter (date-time 2010 10 3))
    
    ;; Get a moment at a specific time on the current date
    (today-at 12 00)
  5. Use cljs-time.core for date-time operations

    master

    The cljs-time.core namespace provides the primary API for creating and manipulating date-time objects.

    Key behaviors:

    • The date-time constructor always returns times in the UTC time zone.
    • Date objects are mutable, but operations that alter a date return a new copy, leaving the original untouched.
    • For dates without time components (to avoid timezone shifting), use local-date or today to get LocalDate instances.
    (use 'cljs-time.core)
    
    ;; Create a DateTime (Year Month Day Hour Minute Second Millisecond)
    (date-time 1986 10 14 4 3 27 456)
    
    ;; Create a DateTime with omitted fields
    (date-time 1986 10 14)
    
    ;; Get current time and Unix epoch
    (now)
    (epoch)
    
    ;; Access fields
    (hour (date-time 1986 10 14 22))
    
    ;; Use LocalDate for dates without time components
    (local-date 2013 3 20)
    
    ;; Relative positioning
    (after? (date-time 1986 10) (date-time 1986 9))
    
    ;; Date arithmetic
    (plus (date-time 1986 10 14) (months 1) (weeks 3))
    
    ;; Intervals
    (def my-interval (interval (date-time 1986) (date-time 1990)))
    (within? my-interval (date-time 1987))
    (overlaps? my-interval (date-time 1987))
    (abuts? my-interval (date-time 1990))
    (in-minutes my-interval)