A few main points about strftime() function
- The strftime() function belongs to datetime and time() modules in Python.
- As the name suggests (“str” “f” “time”), Python strftime() function returns the string representation of the date.
- The strftime() takes an argument where you may specify the date format to be returned.
- The last section shows the useful list of available directives that you may use for formatting dates.
Syntax of using strftime() function
The syntax of using the strftime() function in datetime module is:
By using time module
There is a difference between the syntax in two modules. The strftime() syntax as using time()module is:
The format argument is the same as in the above case i.e. you may use date formatting directives for returning a date in a specified format.
The time.strftime function converts a tuple or struct_time to a string as specified by the format argument. The tuple or struct_time represents a time as returned by localtime() or gmtime() functions.
The next section shows a few examples in different date format by using both modules. First, let me start with the datetime.strftime().
An example of datetime.strftime in DD/MM/YYYY format
The example below only displays the date in the DD/MM/YYYY format. For example, 20/12/2017. The current date is obtained by using the now() function of the datetime module as follows:
#Date formatting in dd/mm/yyyy import datetime curr_date = datetime.datetime.now() print(curr_date.strftime("%d/%m/%Y"))
The output should be like:
The example of date with full day name
This example formats the date in full day name (%A), day number with zero padded decimal (%d), abbreviated month name (%b) and year without the century (%y) as follows:
#Date formatting example import datetime dt_curr = datetime.datetime.now() print(dt_curr.strftime("%A, %d %b %y"))
The example of the current date and time in mm-dd-yyyy format
In this examples, the now() function is used to get the current date and time and strftime() will display in the following format:
#Date formatting example import datetime curr_dt_time = datetime.datetime.now() print("Current Date and Time: ", curr_dt_time.strftime("%m-%d-%Y %H:%M:%S"))
The output should be like this:
List of useful directives with the output
The example below displays the date by using various directives used in the strftime() function.
Again, the current date is used and you can see the directive description towards the left and the output towards the right:
#Date formatting with directives import datetime curr_dt_dir = datetime.datetime.now() print("Date: ", curr_dt_dir, "\n\n") print("Weekday: Abbriviated name e.g. Sun, Mon:: ", curr_dt_dir.strftime("%a")) print("Full Weekday Name e.g. Monday, Tuesday:: ", curr_dt_dir.strftime("%A")) print("Zero padded decinal number day e.g. 01, 02,...31:: ", curr_dt_dir.strftime("%d")) print("Abbreviated Month Name e.g. Jan, Feb:: ", curr_dt_dir.strftime("%b")) print("Full Month Name - March, April etc. :: ", curr_dt_dir.strftime("%B")) print("Month Number - Zero Padded e.g. 01, 02, 12:: ", curr_dt_dir.strftime("%m")) print("Year without century e.g. 01, 02, 99:: ", curr_dt_dir.strftime("%y")) print("Year with Century e.g. 1999, 2018:: ", curr_dt_dir.strftime("Y")) print("24 Hour clock number e.g. 00, 13, 23:: ", curr_dt_dir.strftime("%H")) print("12 Hour Clock e.g. 01, 06, 12:: ", curr_dt_dir.strftime("%I")) print("AM or PM or its locale equivalent:: ", curr_dt_dir.strftime("%p")) print("Minute - Zero padded number e.g. 01, 20, 59:: ", curr_dt_dir.strftime("%M")) print("Zero padded Second - 00, 01, 02, 59 :: ", curr_dt_dir.strftime("%S")) print("Microsecond:: ", curr_dt_dir.strftime("%f")) print("UTC offset in the form ±HHMM[SS[.ffffff]] ::", curr_dt_dir.strftime("%z")) print("Number of day of the year e.g. 001, 100, 366:: ", curr_dt_dir.strftime("%j")) print("Week Number of the year:: ", curr_dt_dir.strftime("%U")) print("Locale's date and time representation:: ", curr_dt_dir.strftime("%c")) print("Weekday Number - 0=Sunday, 6=Saturday:: ", curr_dt_dir.strftime("%w")) print("Date representation accoding to locale:: ", curr_dt_dir.strftime("%x")) print("Time representation accoding to locale:: ", curr_dt_dir.strftime("%X")) print("A literal '%' character.:: ", curr_dt_dir.strftime("%%"))
The output:
An example of time module’s strftime()
In this example, the localtime() function is used in the time’s strftime() function:
#Time Module's strftime() Demo import time t_local = time.localtime() print(time.strftime("%b %d %Y %H:%M:%S",t_local))
The output as I executed this code:
Using gmtime() function example
You may also provide gmtime() function as the second argument as follows:
import time t_gmt = time.gmtime() print(time.strftime("%b %d %Y %H:%M:%S",t_gmt))
The result:
Using various directives in time’s strftime() function
In this example, the mktime() function is used with the strftime() function with almost all available directives:
#Time Module's strftime() Demo import time t = (2018, 12, 2, 4, 45, 10, 3, 22, 0) t = time.mktime(t) print("Weekday: Abbriviated name e.g. Sun, Mon:: ", time.strftime("%a", time.gmtime(t))) print("Full Weekday Name e.g. Monday, Tuesday:: ", time.strftime("%A", time.gmtime(t))) print("Zero padded decinal number day e.g. 01, 02,...31:: ", time.strftime("%d")) print("Abbreviated Month Name e.g. Jan, Feb:: ", time.strftime("%b", time.gmtime(t))) print("Full Month Name - March, April etc. :: ", time.strftime("%B", time.gmtime(t))) print("Month Number - Zero Padded e.g. 01, 02, 12:: ", time.strftime("%m", time.gmtime(t))) print("Year without century e.g. 01, 02, 99:: ", time.strftime("%y", time.gmtime(t))) print("Year with Century e.g. 1999, 2018:: ", time.strftime("Y", time.gmtime(t))) print("24 Hour clock number e.g. 00, 13, 23:: ", time.strftime("%H", time.gmtime(t))) print("12 Hour Clock e.g. 01, 06, 12:: ", time.strftime("%I", time.gmtime(t))) print("AM or PM or its locale equivalent:: ", time.strftime("%p", time.gmtime(t))) print("Minute - Zero padded number e.g. 01, 20, 59:: ", time.strftime("%M", time.gmtime(t))) print("Zero padded Second - 00, 01, 02, 59 :: ", time.strftime("%S", time.gmtime(t))) print("UTC offset in the form ±HHMM[SS[.ffffff]] ::", time.strftime("%z", time.gmtime(t))) print("Number of day of the year e.g. 001, 100, 366:: ", time.strftime("%j", time.gmtime(t))) print("Week Number of the year:: ", time.strftime("%U", time.gmtime(t))) print("Locale's date and time representation:: ", time.strftime("%c", time.gmtime(t))) print("Weekday Number - 0=Sunday, 6=Saturday:: ", time.strftime("%w", time.gmtime(t))) print("Date representation accoding to locale:: ", time.strftime("%x", time.gmtime(t))) print("Time representation accoding to locale:: ", time.strftime("%X", time.gmtime(t))) print("A literal '%' character.:: ", time.strftime("%%", time.gmtime(t)))
The output: