Python strftime()
In this article, we will take a detailed look at strftime()
method of Python’s datetime
module with examples.
strftime()
method is used to convert datetime
object to string in desired format.
Python docs for strftime()
method state
Convert object to a string according to a given format
strftime()
accepts a string argument which represents the format in which datetime
object will be converted to a string. The string argument is a format specifier and is preceded with a %
symbol.
To use strftime()
, we need to import datetime
class from Python’s datetime
module.
Below is an example to convert a
datetime
object to string in various formats. Notice the argument types provided to strftime()
. from datetime import datetime as dt # get current date and time object date_time = dt.now() print('Full date time:',date_time.strftime('%c')) print('Only date:',date_time.strftime('%x')) print('Only time:',date_time.strftime('%X')) print('Complete year:',date_time.strftime('%Y')) print('Year without century:',date_time.strftime('%y')) print('Complete month:',date_time.strftime('%B')) print('Short month:',date_time.strftime('%b')) print('Day short name:',date_time.strftime('%a')) print('Day full name:',date_time.strftime('%A')) print('Date:',date_time.strftime('%d')) print('Hour(24 hour format):',date_time.strftime('%H')) print('Hour(12 hour format):',date_time.strftime('%I')) print('Minute:',date_time.strftime('%M')) print('Second:',date_time.strftime('%S')) print('AM or PM:', date_time.strftime('%p'))
Output is
Full date time: Fri Jul 16 00:11:05 2021
Only date: 07/16/21
Only time: 00:11:05
Complete year: 2021
Year without century: 21
Complete month: July
Short month: Jul
Day short name: Fri
Day full name: Friday
Date: 16
Hour(24 hour format): 00
Hour(12 hour format): 12
Minute: 11
Second: 05
AM or PM: AM
Remember that you can supply any number of format specifiers to strftime()
at once.
Thus, to create a date in custom format, provide individual formats for date, month and year separated with required character as shown below
print('Custom Date format:', date_time.strftime('%d-%m-%Y')) print('Time format:', date_time.strftime('%I:%M:%S %p'))
This prints
Custom Date format: 16-07-2021
Time format: 12:17:22 AM
Following is a list of format codes supported by
strftime()
method Format | Description | Example |
---|---|---|
%a | Short name for Weekday(Locale specific) | Sun, Mon, …, Sat (en_US); So, Mo, …, Sa (de_DE) |
%A | Full name for Weekday(Locale specific) | Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE) |
%w | Numeric value for weekday, with 0 for Sunday, 1 for Monday and so on | 0, 1, …, 6 |
%d | Day of the month as a two digit number. Single value is zero-padded | 01, 02, …, 31 |
%b | Short name for month(Locale specific) | Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE) |
%B | Full name for month(Locale specific) | January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE) |
%m | Month as a two digit number. Single value is zero-padded | 01, 02, …, 12 |
%y | Year without century as two digit number. Single values are zero-padded | 00, 01, …, 99 |
%Y | Year with century as four digit number. Values are padded with 0, if required. | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H | Hour in 24-hour clock. Two digit value padded with zero | 00, 01, …, 23 |
%I | Hour in 12-hour clock. Two digit value padded with zero | 01, 02, …, 12 |
%p | AM or PM(Locale specific) | AM, PM (en_US) am, pm (de_DE) |
%M | Minute as a two digit number. Single value is zero-padded | 00, 01, …, 59 |
%S | Second as a two digit number. Single value is zero-padded | 00, 01, …, 59 |
%f | Microsecond as a six digit number. Single value is zero-padded | 000000, 000001, …, 999999 |
%z | UTC offset in the form ±HHMM[SS[.ffffff]] | (empty), +0000, -0400, +1030, +063415, -030712.345216 |
%Z | Time zone name | (empty), UTC, GMT |
%j | Day of the year as a three digit number, padded with zero, if required. | 001, 002, …, 366 |
%U | Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. | 00, 01, …, 53 |
%W | Week number of the year (Monday as the first day of the week) as a decimal number. | 00, 01, …, 53 |
%c | Complete date and time(Locale specific) | Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE) |
%x | Only date(Locale specific) | 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE) |
%X | Only time(Locale specific) | 21:30:00 (en_US); 21:30:00 (de_DE) |
%% | A literal '%' character. | % |
Most of the formats have been demonstrated in the earlier example. Note that many of these format specifiers return Locale specific values.
strftime()
function to convert datetime
to a string. It can also be used to get current date and time in required format.Hope the article was useful.