/date-clj

A date and time library for clojure

Primary LanguageClojure

date-clj

A date library for clojure loosely based on date.js javascript date library.
date-clj aims to be simple and fun and not a complete and exaustive solution like joda-time. All functions in date-clj are immutable.

Installation

Add date-clj to you project.clj dependencies:

[date-clj "1.0.1"]

Use it on your namespace:

(ns my.namespace (:use [date-clj]))

Usage

The library always returns a java.util.Date in functions that should return some date or time and every
function parameter called 'date' receives a java.util.Date too.

Date creation functions

; timestamp creation - January 02 1970
(date 100000000)

; 2000's Christmas
(date :day 25 :month 11 :year 2000)

; the first sunday of april of the current year, 1:45 PM
(date :week-day :sunday :month :april :hour 13 :minute 45)

; the current date set to december of 1900 (set-date is a immutable function, it returns a new date)
(-> (today) (set-date :month :december :year 1900))

Date math functions

; christmas of this year plus 1 day, 3 months, 5 years and 10 hours
(def xmas (date :day 25 :month :december))
(-> xmas (add 1 :day 3 :months 5 :years 10 :hours))

; 2 weeks and 50 minutes before xmas
(-> xmas (subtract 2 :weeks 50 :minutes))

; 1 year and 5 days from now
(from-now 1 :year 5 :days)

; 3 hours and 200 milliseconds ago
(back 3 :hours 200 :milliseconds)

Relative math functions

; next friday from now
(following :friday)

; the day after xmas
(-> xmas (following :day))

; next january
(following :january)

Date test functions

; is it friday 13? beware!
(-> (today) (is? :friday 13))

; was 3 years and 5 months ago may 2005?
(-> (back 3 :years 5 :months) (was? :may 2005))

; will next year be a leap year?
(-> (following :year) (will-be? :leap-year))

; weekend already?
(-> (today) (is? :weekend))

Date comparison functions

; is xmas before today?
(-> xmas (before? (today)))

; always true
(-> (tomorrow) (after? (yesterday)))

; always true
(-> (today) (between? (yesterday) (tomorrow)))

Date part functions

; 25
(day xmas)

; 11 (months start in 0)
(month xmas)

; a list in the form (day-of-the-week hour minute second) for the current time
(map #(% (today)) [week-day hours minutes seconds])

Specific date functions

; a date representing february of the current year
(february)

; may of the following year
(-> (following :year) may)

; common dates
(today)
(tomorrow)
(yesterday)

; the first sunday of april
(-> (april) sundays first)
; next month's last friday
(-> (following :month) fridays last)

; this week's monday
(monday)

Parsing, formatting and displaying functions

date-clj uses java.text.SimpleDateFormat for parsing and formatting.

; parsing the string into a date using the first matching format
(parse-date "2035.06.23" "yyyy/MM/dd" "yyyy.MM.dd")

; shows "12/24 is christmas eve"
(-> xmas (subtract 1 :day) (format-date "MM/dd 'is christmas eve'"))

; prints the current month's full name in brazilian portuguese
(binding [*locale* (Locale. "pt" "BR")] (format-date (today) "MMMM"))

; a seq of the week day names in german
(binding [*locale* (Locale/GERMAN)] (names :week-days))

; a seq of the short month names in the default locale
(names :months :short)

License

Copyright © 2011 Islon Scherer

Distributed under the Eclipse Public License, the same as Clojure.