Change language

What is the strftime () function in Python?


In this article, we will understand how Python’s strftime () function works along with its variants.

Python has many modules that have clusters of functions to implement various functions with data. The Python time module is used to perform manipulations with different time stamps.

In addition, the Python strftime () function takes time in various forms and returns a string with a time in standard form.
Python Strftime ( )

Option 1: Python strftime ( ) to get the current time

The Python strftime () function can be used in conjunction with the datetime module to get the current timestamp in the proper form according to the format codes.

Syntax: (). strftime (’format codes’) 

So format codes are actually predefined codes used by for correct and standard timestamp representation.


 from datetime import datetime current_timestamp = () tym = current_timestamp.strftime ("% H:% M:% S") date = current_timestamp.strftime ("% d-% m-% Y") print ("Current Time:", tym) print ("Current Date:" , date) 

In the above example, we used the () method  to get the current timestamp, and then passed it to the strftime () function to represent the timestamp in standard format.

We used format codes that have the following meanings:

  • % H &presents & # 171; clock & # 187; in 24-hour format.
  • % M &Decimal representation of minutes.
  • % S &represents the "seconds" portion of the timestamp.


 Current Time: 16:28 : 40 Current Date: 28-04-2020 

Option 2: With a predefined timestamp

Sometimes it happens that we need to display the timestamp of historical datasets. You can do the same with the strftime () function.

The datetime.fromtimestamp () method is used to get a predefined time stamp. In addition, the strftime () function can be used to represent it in standard form using various format codes as explained above.


 datetime.fromtimestamp (timestamp) .strftime () 


 from datetime import datetime given_timestamp = 124579923 timestamp = datetime.fromtimestamp (given_timestamp) tym = timestamp.strftime ("% H:% M:% S") date = timestamp.strftime ("% d-% m-% Y") print ("Time according to the given timestamp: ", tym) print (" Date according to the given timestamp: ", date) 

Exit :

 Time according to the given timestamp: 03:02:03 Date according to the given timestamp: 13-12-1973 

Using different format codes with the strftime () function

strftime () uses format codes to represent the timestamp in standard and supported format. In addition, we can use format codes to separate days, hours, weeks, etc. from timestamps and display them.

Let’s understand the format codes with some examples.

Example 1: Format Code &"% A" to display the current date according to local time.

 from time import strftime day = strftime ("% A") print ("Current day: ", day) 


 Current day: Tuesday 

Example 2: Format code && # 171;% c & # 187; to display the current local time.

Format code && # 8216;% c & # 8217; is used to display the current local time in the following format:

Day Month Date hours: min: sec Year

 from time import strftime day = strftime ("% c") print ("Current timestamp:", day) 


 Current timestamp: Tue Apr 28 16:42:22 2020 

Example 3: Format Code &"% R" to represent the time in 24-hour format.

 from time import strftime day = strftime ("% R") print (" Current time in a 24-hour format: ", day) 


 Current time in a 24-hour format: 16:44 

Example 4: Format Code &"% R" to display the time in H: M: S format along with a description such as AM or PM.

 from time import strftime day = strftime ("% r") print ("Current time - hours: mins: seconds", day) 


 Current time - hours: mins: seconds 05:05:19 PM 

Example 5:

 from time import strftime day = strftime ("% x -% X% p") print ("Local date and time:", day) 

In the above example, we used the format code "% x" to represent the local timestamp in terms of a date and "% X" to represent the local time in the form H: M: S. The format code & # 171;% p & # 187; used to denote whether the timestamp belongs to AM or PM.


 Local date and time: 04 / 28/20 - 17:08:42 PM 


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method