Python DateTime Format Using strftime()

The “datetime” module is used to manipulate date and time in Python. The “datetime” module offers numerous functions to perform different operations on the date and time values. For instance, to represent the datetime in string format the “strftime()” function is used in Python.

In this post, we will explain Python’s “strftime()” function using numerous examples. The below contents will be explained in this post:

How to Format DateTime Using strftime() in Python?

The “strftime()” function converts the DateTime values to string format using character codes. The “strftime()” function syntax is shown below:

datetime_object.strftime(format)

Here in the above syntax, the “datetime_object” specifies the object of date and time that needs to be formatted. The “format” parameter specifies the string that contains format codes and begins with %.

Note: There are various character codes used in the “strftime()” function. You can review all of these in the official Python documentation.

Example 1: Using strftime() to Convert DateTime to String

The below code is utilized to convert the DateTime to string using the “strftime()” function:

Code:

import datetime
time_current = datetime.datetime.now()
print('Current DateTime: ', time_current)
print(type(time_current))

str_value = time_current.strftime("%Y-%m-%d %H:%M:%S")
print('\nString Value of Current DateTime: ',str_value)
print(type(str_value))
  • The “datetime” module is imported in the program.
  • The “datetime.now()” function will retrieve the current datetime.
  • The “strftime()” function takes the string format code and returns the given DateTime into a string representation.

Output:

The above output shows that the given DateTime has been converted into the string format.

Example 2: Using strftime() to Convert Specific DateTime Attributes to String

The following code used the “strftime()” function to convert the particular attributes of the input “DateTime”  to the string format:

Code:

import datetime
time_current = datetime.datetime.now()
print('Current DateTime: ', time_current)
print(type(time_current))

print('\nDate String Value: ', time_current.strftime("%d/%m/%Y"))
print('Month String Value: ', time_current.strftime("%m"))
print('Day String Value: ', time_current.strftime("%d"))
  • The “datetime.now()” function returns the current datetime.
  • The “strftime()” function accepts the character codes according to specific attributes and returns the given datetime into a specific string representation. For example, the “%m” is used to return the month string, and “%d” is used to return the day string value.

Output:

The above output verified that the specific attribute of the given DateTime has been converted into a specific string representation.

Example 3: Using strftime() to Convert timestamp to String

The following code is utilized to convert the input timestamp to a string:

Code:

import datetime, time

time_Stamp = time.time()
print('TimeStamp Value: ', time_Stamp)
time_value = datetime.datetime.fromtimestamp(time_Stamp)
print('DateTime: ', time_value)

print('\nDate String Value: ', time_value.strftime("%d/%m/%Y"))
print('Month String Value: ', time_value.strftime("%m"))
print('Day String Value: ', time_value.strftime("%d"))
  • The “time.time()” function of the time module is used to get the timestamp.
  • The “datetime.fromtimestamp()” function accepts the timestamp and returns the DateTime representation.
  • The “strftime()” function is then used to return the string representation according to the given character codes.

Output:

The timestamp has been converted into a string representation.

Example 4: Using strftime() Function Character Code to Represent Locale’s Appropriate DateTime

The below code utilizes the specific character code for locales appropriate DateTime representation:

Code:

import datetime
time_current = datetime.datetime.now()
print('Current DateTime: ', time_current)

print('\nRepresentation of Locale Appropriate DateTime:')
print('\nDate and Time: ',time_current.strftime("%c"))
print('\nDate Representation: ',time_current.strftime("%x"))
print('\nTime Representation: ',time_current.strftime("%X"))
  • The “strftime()” takes the character code “%c”, “%x” and “%X” to retrieve the date and time representation, date representation and time representation of the locale.

Output:

The above output shows the locale-appropriate datetime representation using a specific character code.

Conclusion

The “strftime()” function is used to represent the DateTime format in string representation using specific standard directives/character codes. The “strftime()” takes the standard directives as an argument and returns the string representation of a given DateTime. We can also convert specific datetime attributes to strings using the “strftime()” function. In this post, we examined various examples related to the “strftime()” function.