date, datetime, and time
objects all support a
strftime(format) method, to create a string
representing the time under the control of an explicit format string. Broadly speaking,
acts like the time module's
d.timetuple()) although not all objects support a timetuple()
For time objects, the format codes for year, month, and day should
not be used, as time objects have no such values. If they're used anyway,
substituted for the year, and
0 for the month and day.
For date objects, the format codes for hours, minutes, and seconds
should not be used, as date objects have no such values. If they're
0 is substituted for them.
For a naive object, the
%Z format codes are replaced by
For an aware object:
- utcoffset() is transformed into a 5-character string of the form
+HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM
is a 2-digit string giving the number of UTC offset minutes. For example, if utcoffset() returns
is replaced with the string
- If tzname() returns
replaced by an empty string. Otherwise
%Z is replaced by the returned value,
which must be a string.
The full set of format codes supported varies across platforms, because Python calls the
platform C library's strftime() function, and platform variations
are common. The documentation for Python's time
module lists the format codes that the C standard (1989 version) requires, and those work on
all platforms with a standard C implementation. Note that the 1999 version of the C standard
added additional format codes.
The exact range of years for which strftime() works also varies
across platforms. Regardless of platform, years before 1900 cannot be used.