Table of contents
1.
Introduction
2.
What is strftime in Python?
3.
Python Strftime() Syntax
4.
How strftime() works?
5.
Strftime() Method in Python Examples
5.1.
Example 1: Basic Formatting
5.2.
Python
5.3.
Example 2: Custom Formatting
5.4.
Python
5.5.
Example 3: Display Time Only
5.6.
Python
6.
Advanced Formatting with strftime
6.1.
Combining Multiple Format Codes
6.2.
Locale’s Appropriate Date and Time Representation:
7.
Real-world Applications of strftime
7.1.
Logging
7.2.
File Naming based on Timestamp
7.3.
Displaying Date and Time in User Interfaces
8.
Frequently Asked Questions
8.1.
What is Python strftime() function?
8.2.
What is time strftime in Python?
8.3.
What module is strftime in Python?
9.
Conclusion
Last Updated: Aug 22, 2025
Medium

Python strftime() function

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The strftime method is a part of the datetime module in Python, utilized for formatting datetime objects into readable strings. This method stands for "string format time," where it takes directives for formatting as arguments and returns a string representing the datetime object in the specified format. 

Strftime

The strftime method is crucial for handling date and time in Python, especially when there's a need to format datetime information in a more human-readable format or prepare it for storage or logging purposes. Whether you are logging events with a timestamp, naming files with a unique timestamp, or presenting date and time information in a user interface, strftime is the tool you'll likely use.

What is strftime in Python?

The strftime method belongs to the datetime class within the datetime module. It accepts one argument - a string which specifies the format. This string contains various format codes that correspond to components of the date and time (like %Y for the year, %m for the month, %d for the day, and so on). Here’s how it looks:

datetime_object.strftime(format)

Where datetime_object is a datetime object, and format is a string containing format codes.

Python Strftime() Syntax

datetime_object.strftime(format)

 

  • datetime_object: The datetime object for which to format the string.
     
  • format: A string specifying the format for the output. It consists of format codes representing various components of the date and time.

How strftime() works?

The strftime() method in Python is used to format a datetime object into a string representation based on the specified format. The format codes in the format string are placeholders for different components like year, month, day, hour, minute, etc.

Strftime() Method in Python Examples

Let us look at examples of strftime() method in Python:

Example 1: Basic Formatting

  • Python

Python

from datetime import datetime

# Basic Formatting
now = datetime.now()
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print("Basic format:", formatted_date)
You can also try this code with Online Python Compiler
Run Code

Output

output

Example 2: Custom Formatting

  • Python

Python

from datetime import datetime

# Custom Format
now = datetime.now()
custom_format = "%A, %B %d, %Y"
formatted_date_custom = now.strftime(custom_format)
print("Custom Format:", formatted_date_custom)
You can also try this code with Online Python Compiler
Run Code

Output

output

Example 3: Display Time Only

  • Python

Python

from datetime import datetime

# Time Only
now = datetime.now()
time_only_format = "%I:%M %p"
formatted_time_only = now.strftime(time_only_format)
print("Time is:", formatted_time_only)
You can also try this code with Online Python Compiler
Run Code

Output

output

Advanced Formatting with strftime

Combining Multiple Format Codes

Combining multiple format codes allows for more detailed formatting. For instance, to get a formatted string that reads like "Tuesday, 15 March 2022 02:30PM", you can combine the aforementioned format codes as shown below:

detailed_format = now.strftime("%A, %d %B %Y %I:%M%p")
print(detailed_format)  # Output: 'Tuesday, 15 March 2022 02:30PM' (for example)

Locale’s Appropriate Date and Time Representation:

The %c format code provides a locale's appropriate date and time representation. It's useful when you need to format date and time according to the conventions of the current locale:

locale_format = now.strftime("%c")
print(locale_format)  # Output: 'Tue Mar 15 14:30:45 2022' (for example)

Real-world Applications of strftime

Logging

In real-world applications, logging is critical for debugging and monitoring. Timestamps are often added to log messages to track the sequence of events. The strftime method can be used to format these timestamps.

import datetime
now = datetime.datetime.now()
formatted_timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
log_message = f"{formatted_timestamp} - Log message"
print(log_message)  # Output: '2022-11-01 15:32:10 - Log message'

File Naming based on Timestamp

File naming with timestamps helps in versioning and organizing files. This is especially useful in backup operations.

backup_filename = now.strftime("backup_%Y%m%d_%H%M%S.txt")
print(backup_filename)  # Output: 'backup_20221101_153210.txt'

Displaying Date and Time in User Interfaces

Displaying formatted date and time is a common requirement in user interfaces. strftime provides a way to present date and time in a user-friendly manner.

user_friendly_date = now.strftime("%A, %d %B %Y")
print(user_friendly_date)  # Output: 'Tuesday, 01 November 2022'

Frequently Asked Questions

What is Python strftime() function?

The strftime() function in Python is a method used to format datetime objects into strings, allowing customization of the output based on format codes.

What is time strftime in Python?

The strftime function is commonly used with the datetime module in Python to format date and time values as strings.

What module is strftime in Python?

The strftime function is part of the datetime module in Python, providing functionality to work with dates and times.

Conclusion

In this article, we delved into the strftime method in Python, explored its syntax, and looked at various real-world applications. By sticking to best practices such as using ISO 8601 format and handling locales and timezones correctly, you can avoid common pitfalls and get the most out of strftime. It's encouraged to experiment with strftime and explore its versatility in handling date and time in Python to improve your projects and applications.

Recommended Readings:

Live masterclass