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:
Python
import time
t = time.localtime()
fmt_time = time.strftime("%Y-%m-%d %H:%M:%S", t) # format time
print(fmt_time)
2025-03-08 11:01:08
Syntax:
time.strftime(format[, t])
Parameters:
Returns: Returns a formatted string representing the time.
Points to remember:
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4