A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on. This matches the definition of the "proleptic Gregorian" calendar in Dershowitz and Reingold's book Calendrical Calculations, where it's the base calendar for all computations. See the book for algorithms for converting between proleptic Gregorian ordinals and many other calendar systems.
year, month, day) |
MINYEAR <= year <= MAXYEAR
1 <= month <= 12
1 <= day <= number of days in the given month and year
If an argument outside those ranges is given, ValueError is raised.
Other constructors, all class methods:
) |
date.fromtimestamp(time.time())
.
timestamp) |
ordinal) |
1 <= ordinal <= date.max.toordinal()
.
For any date d, date.fromordinal(d.toordinal()) ==
d
.
Class attributes:
date(MINYEAR, 1, 1)
.
date(MAXYEAR, 12, 31)
.
timedelta(days=1)
.
Instance attributes (read-only):
Supported operations:
Operation | Result |
---|---|
date2 = date1 + timedelta |
date2 is timedelta.days days removed from
date1. (1) |
date2 = date1 - timedelta |
Computes date2 such that date2 + timedelta
== date1 . (2) |
timedelta = date1 - date2 |
(3) |
date1 < date2 |
date1 is considered less than date2 when date1 precedes date2 in time. (4) |
Notes:
timedelta.days
> 0
, or backward if timedelta.days < 0
. Afterward
date2 - date1 == timedelta.days
.
timedelta.seconds
and
timedelta.microseconds
are ignored.
OverflowError is raised if date2.year
would be smaller than MINYEAR or larger than
MAXYEAR.
timedelta.seconds
and timedelta.microseconds
are ignored.
date1 < date2
if and only if date1.toordinal() <
date2.toordinal()
.
In order to stop comparison from falling back to the default
scheme of comparing object addresses, date comparison
normally raises TypeError if the other comparand
isn't also a date object. However, NotImplemented
is returned instead if the other comparand has a
timetuple attribute. This hook gives other kinds of
date objects a chance at implementing mixed-type comparison.
If not, when a date object is
compared to an object of a different type, TypeError is
raised unless the comparison is ==
or !=
. The latter
cases return False or True, respectively.
Dates can be used as dictionary keys. In Boolean contexts, all date objects are considered to be true.
Instance methods:
year, month, day) |
d == date(2002, 12, 31)
, then
d.replace(day=26) == date(2002, 12, 26)
.
) |
d.timetuple()
is equivalent to
time.struct_time((d.year, d.month, d.day,
0, 0, 0,
d.weekday(),
d.toordinal() - date(d.year, 1, 1).toordinal() + 1,
-1))
) |
date.fromordinal(d.toordinal()) == d
.
) |
date(2002, 12, 4).weekday() == 2
, a
Wednesday.
See also isoweekday().
) |
date(2002, 12, 4).isoweekday() == 3
, a
Wednesday.
See also weekday(), isocalendar().
) |
The ISO calendar is a widely used variant of the Gregorian calendar. See http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm for a good explanation.
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.
For example, 2004 begins on a Thursday, so the first week of ISO
year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
2004, so that
date(2003, 12, 29).isocalendar() == (2004, 1, 1)
and
date(2004, 1, 4).isocalendar() == (2004, 1, 7)
.
) |
date(2002, 12, 4).isoformat() == '2002-12-04'
.
) |
str(d)
is equivalent to
d.isoformat()
.
) |
d.ctime()
is equivalent to
time.ctime(time.mktime(d.timetuple()))
on platforms where the native C ctime() function
(which time.ctime() invokes, but which
date.ctime() does not invoke) conforms to the C standard.
format) |
See About this document... for information on suggesting changes.