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.
-
date( ) - Return date object with same year, month and day.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.date() # datetime.date(2008, 3, 5)
-
time( ) - Return time object with same hour, minute, second and microsecond. tzinfo is None. See also method timetz().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.time() # datetime.time(5, 17)
-
timetz( ) - Return time object with same hour, minute, second, microsecond, and tzinfo members. See also method time().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.timetz() # datetime.time(5, 17)
-
replace( [year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]]) - Return a datetime with the same members, except for those members given new values by whichever keyword arguments are specified. Note that tzinfo=None can be specified to create a naive datetime from an aware datetime with no conversion of date and time members.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.replace(hour=7) # datetime.datetime(2008, 3, 5, 7, 17)
-
timetuple( ) - Return a time.struct_time such as returned by time.localtime(). d.timetuple() is equivalent to time.struct_time((d.year, d.month, d.day, d.hour, d.minute, d.second, d.weekday(), d.toordinal() - date(d.year, 1, 1).toordinal() + 1, dst)) The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.timetuple() # (2008, 3, 5, 5, 17, 0, 2, 65, -1)
-
utctimetuple( ) - If datetime instance d is naive, this is the same as d.timetuple() except that tm_isdst is forced to 0 regardless of what d.dst() returns. DST is never in effect for a UTC time.
If d is aware, d is normalized to UTC time, by subtracting d.utcoffset(), and a time.struct_time for the normalized time is returned. tm_isdst is forced to 0. Note that the result's tm_year member may be MINYEAR-1 or MAXYEAR+1, if d.year was MINYEAR or MAXYEAR and UTC adjustment spills over a year boundary.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.utctimetuple() # (2008, 3, 5, 5, 17, 0, 2, 65, 0)
-
toordinal( ) - Return the proleptic Gregorian ordinal of the date. The same as self.date().toordinal().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.toordinal() # 733106
-
weekday( ) - Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as self.date().weekday(). See also isoweekday().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.weekday() # 2
-
isoweekday( ) - Return the day of the week as an integer, where Monday is 1 and Sunday is 7. The same as self.date().isoweekday(). See also weekday(), isocalendar().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.isoweekday() # 3
-
isocalendar( ) - Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The same as self.date().isocalendar().
d # datetime.datetime(2008, 3, 5, 5, 17)
d.isocalendar() # (2008, 10, 3)
-
isoformat( [sep]) - Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0, YYYY-MM-DDTHH:MM:SS
If utcoffset() does not return None, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM
d # datetime.datetime(2008, 3, 5, 5, 17)
d.isoformat() # '2008-03-05T05:17:00'
-
__str__( ) - For a datetime instance d, str(d) is equivalent to d.isoformat(' ').
d # datetime.datetime(2008, 3, 5, 5, 17)
d.__str__() # '2008-03-05 05:17:00'
-
ctime( ) - Return a string representing the date and time, for example datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 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 datetime.ctime() does not invoke) conforms to the C standard.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.ctime() # 'Wed Mar 5 05:17:00 2008'
-
strftime( format) - Return a string representing the date and time, controlled by an explicit format string. See section 5.1.7 - strftime() behavior.
d # datetime.datetime(2008, 3, 5, 5, 17)
d.strftime('%a %b %d') # 'Wed Mar 05'
index
