PBE logo

Datetime.date Module - Basic date and time typesΒΆ

The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient member extraction for output formatting and manipulation. replace( year, month, day) - Return a date with the same value, except for those members given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).

timetuple( ) - Return a time.struct_time such as returned by time.localtime(). The hours, minutes and seconds are 0, and the DST flag is -1. 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))

toordinal( ) - Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any date object d, date.fromordinal(d.toordinal()) == d.

weekday( ) - Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday. See also isoweekday().

isoweekday( ) - Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a Wednesday. See also weekday(), isocalendar().

isocalendar( ) - Return a 3-tuple, (ISO year, ISO week number, ISO weekday). 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.

isoformat( ) - Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example, date(2002, 12, 4).isoformat() == ‘2002-12-04’. 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).

__str__( ) - For a date d, str(d) is equivalent to d.isoformat().

ctime( ) - Return a string representing the date, for example date(2002, 12, 4).ctime() == ‘Wed Dec 4 00:00:00 2002’. 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.

strftime( format) - Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. See section 5.1.7 - strftime() behavior.

Examples:

d                       # datetime.date(2008, 3, 5)
d.today()               # datetime.date(2008, 4, 1)
d.replace(day=20)       # datetime.date(2008, 3, 20)
d.timetuple()           # (2008, 3, 5, 0, 0, 0, 2, 65, -1)
d.toordinal()           # 733106
d.weekday()             # 2
d.isoweekday()          # 3
d.isocalendar()         # (2008, 10, 3)
d.isoformat()           # '2008-03-05'
d.__str__()             # '2008-03-05'
d.ctime()               # 'Wed Mar 5 00:00:00 2008'
d.strftime('%a %b %d')  # 'Wed Mar 05'