clj-time.core

The core namespace for date-time operations in the clj-time library.

Create a DateTime instance with date-time (or a LocalDateTime instance with local-date-time),
specifying the year, month, day, hour, minute, second, and millisecond:

  => (date-time 1986 10 14 4 3 27 456)
  #<DateTime 1986-10-14T04:03:27.456Z>

  => (local-date-time 1986 10 14 4 3 27 456)
  #<LocalDateTime 1986-10-14T04:03:27.456>

Less-significant fields can be omitted:

  => (date-time 1986 10 14)
  #<DateTime 1986-10-14T00:00:00.000Z>

  => (local-date-time 1986 10 14)
  #<LocalDateTime 1986-10-14T00:00:00.000>

Get the current time with (now) and the start of the Unix epoch with (epoch).

Once you have a date-time, use accessors like hour and second to access the
corresponding fields:

  => (hour (date-time 1986 10 14 22))
  22

  => (hour (local-date-time 1986 10 14 22))
  22

The date-time constructor always returns times in the UTC time zone. If you
want a time with the specified fields in a different time zone, use
from-time-zone:

  => (from-time-zone (date-time 1986 10 22) (time-zone-for-offset -2))
  #<DateTime 1986-10-22T00:00:00.000-02:00>

If on the other hand you want a given absolute instant in time in a
different time zone, use to-time-zone:

  => (to-time-zone (date-time 1986 10 22) (time-zone-for-offset -2))
  #<DateTime 1986-10-21T22:00:00.000-02:00>

In addition to time-zone-for-offset, you can use the time-zone-for-id and
default-time-zone functions and the utc Var to construct or get DateTimeZone
instances.

The functions after? and before? determine the relative position of two
DateTime instances:

  => (after? (date-time 1986 10) (date-time 1986 9))
  true

  => (after? (local-date-time 1986 10) (local-date-time 1986 9))
  true

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))
  #<DateTime 1986-12-05T00:00:00.000Z>

  => (plus (local-date-time 1986 10 14) (months 1) (weeks 3))
  #<LocalDateTime 1986-12-05T00:00:00.000Z>

An Interval is used to represent the span of time between two DateTime
instances. Construct one using interval, then query them using within?,
overlaps?, and abuts?

  => (within? (interval (date-time 1986) (date-time 1990))
              (date-time 1987))
  true

To find the amount of time encompassed by an interval, use in-seconds and
in-minutes:

  => (in-minutes (interval (date-time 1986 10 2) (date-time 1986 10 14)))
  17280

The overlap function can be used to get an Interval representing the
overlap between two intervals:

  => (overlap (t/interval (t/date-time 1986) (t/date-time 1990))
                          (t/interval (t/date-time 1987) (t/date-time 1991)))
  #<Interval 1987-01-01T00:00:00.000Z/1990-01-01T00:00:00.000Z>

Note that all functions in this namespace work with Joda objects or ints. If
you need to print or parse date-times, see clj-time.format. If you need to
coerce date-times to or from other types, see clj-time.coerce.

abuts?

(abuts? i-a i-b)
Returns true if Interval i-a abuts i-b, i.e. then end of i-a is exactly the
beginning of i-b.

ago

(ago period)
Returns a DateTime a supplied period before the present.
e.g. (-> 5 years ago)

available-ids

(available-ids)
Returns a set of available IDs for use with time-zone-for-id.

date-midnight

(date-midnight year)(date-midnight year month)(date-midnight year month day)
Constructs and returns a new DateMidnight in UTC.
Specify the year, month of year, day of month. Note that month and day are
1-indexed. Any number of least-significant components can be ommited, in which case
they will default to 1.

date-time

(date-time year)(date-time year month)(date-time year month day)(date-time year month day hour)(date-time year month day hour minute)(date-time year month day hour minute second)(date-time year month day hour minute second millis)
Constructs and returns a new DateTime in UTC.
Specify the year, month of year, day of month, hour of day, minute of hour,
second of minute, and millisecond of second. Note that month and day are
1-indexed while hour, second, minute, and millis are 0-indexed.
Any number of least-significant components can be ommited, in which case
they will default to 1 or 0 as appropriate.

DateTimeProtocol

protocol

Interface for various date time functions

members

after?

(after? this that)
Returns true if ReadableDateTime 'this' is strictly after date/time 'that'.

before?

(before? this that)
Returns true if ReadableDateTime 'this' is strictly before date/time 'that'.

day

(day this)
Return the day of month component of the given date/time.

day-of-week

(day-of-week this)
Return the day of week component of the given date/time. Monday is 1 and Sunday is 7

equal?

(equal? this that)
Returns true if ReadableDateTime 'this' is strictly equal to date/time 'that'.

first-day-of-the-month-

(first-day-of-the-month- this)
Returns the first day of the month

hour

(hour this)
Return the hour of day component of the given date/time. A time of 12:01am will have an hour component of 0.

last-day-of-the-month-

(last-day-of-the-month- this)
Returns the last day of the month

milli

(milli this)
Return the millisecond of second component of the given date/time.

minus-

(minus- this period)
Returns a new date/time corresponding to the given date/time moved backwards by the given Period(s).

minute

(minute this)
Return the minute of hour component of the given date/time.

month

(month this)
Return the month component of the given date/time.

plus-

(plus- this period)
Returns a new date/time corresponding to the given date/time moved forwards by the given Period(s).

sec

(sec this)
Return the second of minute component of the given date/time.

second

(second this)
Return the second of minute component of the given date/time.

week-number-of-year

(week-number-of-year this)
Returs the number of weeks in the year

year

(year this)
Return the year component of the given date/time.

days

(days)(days n)
Given a number, returns a Period representing that many days.
Without an argument, returns a PeriodType representing only days.

days?

(days? val)
Returns true if the given value is an instance of Days

default-time-zone

(default-time-zone)
Returns the default DateTimeZone for the current environment.

deprecated

(deprecated message)

do-at

macro

(do-at base-date-time & body)
Like clojure.core/do except evalautes the expression at the given date-time

do-at*

(do-at* base-date-time body-fn)

earliest

(earliest dt1 dt2)(earliest dts)
Returns the earliest of the supplied DateTimes

end

(end in)
Returns the end DateTime of an Interval.

epoch

(epoch)
Returns a DateTime for the beginning of the Unix epoch in the UTC time zone.

extend

(extend in & by)
Returns an Interval with an end ReadableDateTime the specified Period after the end
of the given Interval

first-day-of-the-month

(first-day-of-the-month year month)(first-day-of-the-month dt)

floor

(floor dt dt-fn)
Floors the given date-time dt to the given time unit dt-fn,
e.g. (floor (now) hour) returns (now) for all units
up to and including the hour

from-now

(from-now period)
Returns a DateTime a supplied period after the present.
e.g. (-> 30 minutes from-now)

from-time-zone

(from-time-zone dt tz)
Returns a new ReadableDateTime corresponding to the same point in calendar time as
the given ReadableDateTime, but for a correspondingly different absolute instant in
time.

hours

(hours)(hours n)
Given a number, returns a Period representing that many hours.
Without an argument, returns a PeriodType representing only hours.

hours?

(hours? val)
Returns true if the given value is an instance of Hours

in-msecs

deprecated in 0.6.0

(in-msecs in)
DEPRECATED: Returns the number of milliseconds in the given Interval.

in-secs

deprecated in 0.6.0

(in-secs in)
DEPRECATED: Returns the number of standard seconds in the given Interval.

interval

(interval dt-a dt-b)
Returns an interval representing the span between the two given ReadableDateTimes.
Note that intervals are closed on the left and open on the right.

InTimeUnitProtocol

protocol

Interface for in-<time unit> functions

members

in-days

(in-days this)
Return the time in days.

in-hours

(in-hours this)
Return the time in hours.

in-millis

(in-millis this)
Return the time in milliseconds.

in-minutes

(in-minutes this)
Return the time in minutes.

in-months

(in-months this)
Return the time in months

in-seconds

(in-seconds this)
Return the time in seconds.

in-weeks

(in-weeks this)
Return the time in weeks

in-years

(in-years this)
Return the time in years

last-day-of-the-month

(last-day-of-the-month year month)(last-day-of-the-month dt)

latest

(latest dt1 dt2)(latest dts)
Returns the latest of the supplied DateTimes

local-date

(local-date year month day)
Constructs and returns a new LocalDate.
Specify the year, month, and day. Does not deal with timezones.

local-date-time

(local-date-time year)(local-date-time year month)(local-date-time year month day)(local-date-time year month day hour)(local-date-time year month day hour minute)(local-date-time year month day hour minute second)(local-date-time year month day hour minute second millis)
Constructs and returns a new LocalDateTime.
Specify the year, month of year, day of month, hour of day, minute of hour,
second of minute, and millisecond of second. Note that month and day are
1-indexed while hour, second, minute, and millis are 0-indexed.
Any number of least-significant components can be ommited, in which case
they will default to 1 or 0 as appropriate.

local-time

(local-time hour)(local-time hour minute)(local-time hour minute second)(local-time hour minute second millis)
Constructs and returns a new LocalTime.
Specify the hour of day, minute of hour, second of minute, and millisecond of second.
Any number of least-significant components can be ommited, in which case
they will default to 1 or 0 as appropriate.

max-date

(max-date dt & dts)
Maximum of the provided DateTimes.

millis

(millis)(millis n)
Given a number, returns a Period representing that many milliseconds.
Without an argument, returns a PeriodType representing only milliseconds.

min-date

(min-date dt & dts)
Minimum of the provided DateTimes.

mins-ago

(mins-ago d)

minus

(minus dt p)(minus dt p & ps)
Returns a new date/time object corresponding to the given date/time moved backwards by
the given Period(s).

minutes

(minutes)(minutes n)
Given a number, returns a Period representing that many minutes.
Without an argument, returns a PeriodType representing only minutes.

minutes?

(minutes? val)
Returns true if the given value is an instance of Minutes

months

(months)(months n)
Given a number, returns a Period representing that many months.
Without an argument, returns a PeriodType representing only months.

months?

(months? val)
Returns true if the given value is an instance of Months

now

(now)
Returns a DateTime for the current instant in the UTC time zone.

nth-day-of-the-month

(nth-day-of-the-month year month n)(nth-day-of-the-month dt n)
Returns the nth day of the month.

number-of-days-in-the-month

(number-of-days-in-the-month dt)(number-of-days-in-the-month year month)

overlap

(overlap i-a i-b)
Returns an Interval representing the overlap of the specified Intervals.
Returns nil if the Intervals do not overlap.
The first argument must not be nil.
If the second argument is nil then the overlap of the first argument
and a zero duration interval with both start and end times equal to the
current time is returned.

overlaps?

(overlaps? i-a i-b)(overlaps? start-a end-a start-b end-b)
With 2 arguments: Returns true of the two given Intervals overlap.
Note that intervals that satisfy abuts? do not satisfy overlaps?
With 4 arguments: Returns true if the range specified by start-a and end-a
overlaps with the range specified by start-b and end-b.

plus

(plus dt p)(plus dt p & ps)
Returns a new date/time corresponding to the given date/time moved forwards by
the given Period(s).

seconds

(seconds)(seconds n)
Given a number, returns a Period representing that many seconds.
Without an argument, returns a PeriodType representing only seconds.

seconds?

(seconds? val)
Returns true if the given value is an instance of Seconds

secs

deprecated in 0.6.0

(secs)(secs n)
DEPRECATED

secs?

deprecated in 0.6.0

(secs? val)
DEPRECATED

start

(start in)
Returns the start DateTime of an Interval.

time-now

(time-now)
Returns a LocalTime for the current instant without date or time zone
using ISOChronology in the current time zone.

time-zone-for-id

(time-zone-for-id id)
Returns a DateTimeZone for the given ID, which must be in long form, e.g.
'America/Matamoros'.

time-zone-for-offset

(time-zone-for-offset hours)(time-zone-for-offset hours minutes)
Returns a DateTimeZone for the given offset, specified either in hours or
hours and minutes.

to-time-zone

(to-time-zone dt tz)
Returns a new ReadableDateTime corresponding to the same absolute instant in time as
the given ReadableDateTime, but with calendar fields corresponding to the given
TimeZone.

today

(today)
Constructs and returns a new LocalDate representing today's date.
LocalDate objects do not deal with timezones at all.

today-at

(today-at hours minutes seconds millis)(today-at hours minutes seconds)(today-at hours minutes)

today-at-midnight

deprecated in 0.12.0

(today-at-midnight)(today-at-midnight tz)
DEPRECATED: Please use with-time-at-start-of-day instead. See http://goo.gl/nQCmKd
Returns a DateMidnight for today at midnight in the UTC time zone.

utc

DateTimeZone for UTC.

weeks

(weeks)(weeks n)
Given a number, returns a Period representing that many weeks.
Without an argument, returns a PeriodType representing only weeks.

weeks?

(weeks? val)
Returns true if the given value is an instance of Weeks

with-time-at-start-of-day

(with-time-at-start-of-day dt)
Returns a DateTime representing the start of the day. Normally midnight,
but not always true, as in some time zones with daylight savings.

within?

(within? i dt)(within? start end test)
With 2 arguments: Returns true if the given Interval contains the given
ReadableDateTime. Note that if the ReadableDateTime is exactly equal to the
end of the interval, this function returns false.
With 3 arguments: Returns true if the start ReadablePartial is
equal to or before and the end ReadablePartial is equal to or after the test
ReadablePartial.

year-month

(year-month year)(year-month year month)
Constructs and returns a new YearMonth.
Specify the year and month of year. Month is 1-indexed and defaults
to January (1).

years

(years)(years n)
Given a number, returns a Period representing that many years.
Without an argument, returns a PeriodType representing only years.

years?

(years? val)
Returns true if the given value is an instance of Years

yesterday

(yesterday)
Returns a DateTime for yesterday relative to now