Date and Time
The 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.
Here is a list of the format codes with their directive and meaning.
- How to use Date and Time in Python
- Python Programming – Date and Time Functions
- What is The Difference Between __Str__ And __Repr__ in Python
%a Locale’s abbreviated weekday name. %A Locale’s full weekday name. %b Locale’s abbreviated month name. %B Locale’s full month name. %c Locale’s appropriate date and time representation. %d Day of the month as a decimal number [01,31]. %f Microsecond as a decimal number [0,999999], zero-padded on the left %H Hour (24-hour clock) as a decimal number [00,23]. %I Hour (12-hour clock) as a decimal number [01,12]. %j Day of the year as a decimal number [001,366]. %m Month as a decimal number [01,12]. %M Minute as a decimal number [00,59]. %p Locale’s equivalent of either AM or PM. %S Second as a decimal number [00,61]. %U Week number of the year (Sunday as the first day of the week) %w Weekday as a decimal number [0(Sunday),6]. %W Week number of the year (Monday as the first day of the week) %x Locale’s appropriate date representation. %X Locale’s appropriate time representation. %y Year without century as a decimal number [00,99]. %Y Year with century as a decimal number. %z UTC offset in the form +HHMM or -HHMM. %Z Time zone name (empty string if the object is naive). %% A literal '%' character.
strftime() vs strptime()
strptime() – String “Parse” Time – used to convert a string to a Date/Time object. Use this to parse a date string into a Date/Time object.
strftime() – String “Format” Time – used to format a Date object. Use this when you want to format a date.