VOOZH about

URL: https://www.geeksforgeeks.org/python/time-strftime-function-in-python/

⇱ time.strftime() function in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

time.strftime() function in Python

Last Updated : 11 Jul, 2025

time.strftime(format[, t]) function in Python's time module converts a given time tuple (t) or struct_time object into a formatted string based on the specified format. If t is not provided, the function uses the current local time. The format must be a string and a ValueError is raised if any field in t is out of range. Example:


Output
2025-03-08 11:01:08

Syntax:

time.strftime(format[, t])

Parameters:

  • format (str): string containing format codes that define how the output should be structured.
  • t (optional): time structure (e.g., from gmtime() or localtime()). If omitted, the current local time is used.

Returns: Returns a formatted string representing the time.

Points to remember:

  • 0 is a valid argument for any position in the time tuple, if it is normally illegal, the value is adjusted to a correct one.
  • When used with the strptime(), the %p directive only affects the hour field if %I is also used.
  • The range of seconds can be 0-61 due to leap seconds.
  • %U and %W affect calculations only when the day of the week and year are specified.

Commonly used directives in strftime()

Directive

Meaning

Example Output

%a

Abbreviated weekday name

Tue

%A

Full weekday name

Tuesday

%b

Abbreviated month name

Jun

%B

Full month name

June

%c

Preferred date and time representation

Tue Jun 25 10:09:52 2019

%d

Day of the month (01-31)

25

%H

Hour (00-23)

10

%I

Hour (01-12)

10

%j

Day of the year (001-366)

176

%m

Month (01-12)

06

%M

Minute (00-59)

09

%S

Second (00-59)

52

%U

Week number (Sunday as the first day)

25

%W


Week number (Monday as the first day)

25

%Y

Year with century

2019

%y

Year without century (00-99)

19

%Z

Time zone name

UTC

%z

UTC offset

+0000

Example:

Output

Example 1: Sat, 08 Mar 2025 11:00:10 + 1010
Example 2: Saturday, 03/08/25 March 2025 11:00:10 + 0000
Example 3: Sat Mar 8 11:00:10 2025
Example 4: 20
Example 5: Saturday, 03/08/25 March 2025, 11:00:10 AM,
MOTY:03
DOTY:% j
Example 6: 11:00
Example 7: Sat, 08 Mar 2025 11:00:10 + 0000
Example 8: 11:00:10 AM, 11:00:10
Example 9: 11:00:10 AM, 6, 09
Example 10: 11:00:10 AM, 10, 09, 6
Example 11: 03/08/25, 11:00:10, 25, 2025
Example 12: 11:00:10 AM, +0000, UTC
Comment
Article Tags: