py4u blog

Mastering the `time.strftime()` Function in Python

In Python, handling dates and times is a common task in various applications, from simple scripts to complex web applications. The time module provides a range of functions to work with time-related data. One of the most useful functions in this module is time.strftime(). This function allows you to format time objects into strings according to a specified format. In this blog post, we will explore the time.strftime() function in detail, including its syntax, common format codes, example usage, and best practices.

2026-07

Table of Contents#

  1. Syntax of time.strftime()
  2. Common Format Codes
  3. Example Usage
  4. Best Practices
  5. Limitations and Considerations
  6. Conclusion
  7. References

Syntax of time.strftime()#

The time.strftime() function has the following syntax:

time.strftime(format[, t])
  • format: This is a string that specifies the format in which the time should be represented. It contains special format codes that are replaced with the corresponding time values.
  • t (optional): This is a tuple representing a time in the struct_time format. If this argument is not provided, the current local time is used.

Common Format Codes#

Here are some of the most commonly used format codes in time.strftime():

Year#

  • %Y: Full year (e.g., 2023)
  • %y: Abbreviated year (e.g., 23)

Month#

  • %B: Full month name (e.g., January)
  • %b: Abbreviated month name (e.g., Jan)
  • %m: Month as a zero-padded decimal number (e.g., 01)

Day#

  • %d: Day of the month as a zero-padded decimal number (e.g., 01)
  • %A: Full weekday name (e.g., Monday)
  • %a: Abbreviated weekday name (e.g., Mon)

Time#

  • %H: Hour in 24-hour format as a zero-padded decimal number (e.g., 00 - 23)
  • %I: Hour in 12-hour format as a zero-padded decimal number (e.g., 01 - 12)
  • %p: AM/PM indicator (e.g., AM or PM)
  • %M: Minutes as a zero-padded decimal number (e.g., 00 - 59)
  • %S: Seconds as a zero-padded decimal number (e.g., 00 - 59)

Other#

  • %c: Locale's appropriate date and time representation
  • %x: Locale's appropriate date representation
  • %X: Locale's appropriate time representation

Example Usage#

Example 1: Formatting the Current Time#

import time
 
# Get the current local time
current_time = time.localtime()
 
# Format the time as "Year-Month-Day Hour:Minute:Second"
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)
print(formatted_time)

In this example, we first get the current local time using time.localtime(). Then, we use time.strftime() to format the time as a string in the "Year-Month-Day Hour:Minute:Second" format.

Example 2: Formatting a Specific Time#

import time
 
# Create a struct_time object representing a specific time
specific_time = time.struct_time((2023, 10, 15, 14, 30, 0, 6, 288, 0))
 
# Format the time as "Month Day, Year - Hour:Minute AM/PM"
formatted_time = time.strftime("%B %d, %Y - %I:%M %p", specific_time)
print(formatted_time)

In this example, we create a struct_time object representing a specific time. Then, we use time.strftime() to format the time as a string in the "Month Day, Year - Hour:Minute AM/PM" format.

Best Practices#

  • Use Descriptive Format Strings: When using time.strftime(), use format strings that are easy to read and understand. This will make your code more maintainable.
  • Handle Time Zones: If your application needs to handle different time zones, be aware that time.strftime() uses the local time by default. You may need to use other modules like datetime and pytz to handle time zones correctly.
  • Error Handling: When working with time formatting, make sure to handle potential errors. For example, if the format string contains an invalid format code, time.strftime() will raise a ValueError.

Limitations and Considerations#

  • Locale Dependence: The output of time.strftime() depends on the locale settings of the system. This means that the names of months and weekdays may vary depending on the user's locale.
  • Limited to Local Time: By default, time.strftime() uses the local time. If you need to work with UTC or other time zones, you may need to use other functions or modules.

Conclusion#

The time.strftime() function is a powerful tool for formatting time objects into strings in Python. By understanding its syntax, common format codes, and best practices, you can easily format time data according to your needs. Whether you are working on a simple script or a complex application, time.strftime() can help you present time information in a user-friendly way.

References#