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.
Add date-clj to you project.clj dependencies:
Use it on your namespace:
(ns my.namespace (:use [date-clj]))
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
; 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
; the day after xmas
(-> xmas (following :day))
; next 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
; 11 (months start in 0)
; 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
; may of the following year
(-> (following :year) may)
; common dates
; the first sunday of april
(-> (april) sundays first)
; next month's last friday
(-> (following :month) fridays last)
; this week's 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)
Copyright © 2011 Islon Scherer
Distributed under the Eclipse Public License, the same as Clojure.