Open In App

time.strftime() function in Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Python
import time

t = time.localtime()
fmt_time = time.strftime("%Y-%m-%d %H:%M:%S", t) # format time

print(fmt_time)

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:

Python
from time import gmtime, strftime

# using simple format of showing time
s = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime())
print("Example 1:", s)

# only change in this is the full names
# and the representation
s = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime())
print("Example 2:", s)

# this will show you the preferred date time format
s = strftime("%c")
print("Example 3:", s)

# this will tell about the centuries
s = strftime("%C")
print("Example 4:", s)

# MOTY: month of the year
# DOTY: Day of the year
# Simple representation
# % n - new line
s = strftime("%A, %D %B %Y, %r, %nMOTY:%m %nDOTY:% j")
print("Example 5:", s)


# % R - time in 24 hour notation
s = strftime(" %R ")
print("Example 6:", s)


# % H - hour, using a 24-hour clock (00 to 23) in Example 1, 2, 3
# % I - hour, using a 12-hour clock (01 to 12)
s = strftime("%a, %d %b %Y %I:%M:%S + 0000", gmtime())
print("Example 7:", s)

# % T - current time, equal to % H:% M:% S
s = strftime("%r, %T ", gmtime())
print("Example 8:", s)

# % u an % U use (see difference)
s = strftime("%r, %u, %U")
print("Example 9:", s)

# use of % V, % W, % w
s = strftime("%r, %V, %W, %w")
print("Example 10:", s)

# use of % x, % X, % y, % Y
s = strftime("%x, %X, %y, %Y")
print("Example 11:", s)

# use of % Z, % z
s = strftime("%r, %z, %Z")
print("Example 12:", s)

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

Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy