The Wayback Machine - https://web.archive.org/web/20241212102016/https://www.geeksforgeeks.org/time-strftime-function-in-python/
Open In App

time.strftime() function in Python

Last Updated : 03 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

As time module provides various time-related functions. So it is necessary to import the time module otherwise it will through error because of the definition of time.strftime(format[, t]) is present in time module.
time.strftime(format[, t]) function convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. 
If t is not provided, the current time as returned by localtime() is used. The format must be a string. ValueError is raised if any field in t is outside of the allowed range.
Note: 
0 is a legal argument for any position in the time tuple; if it is normally illegal the value is forced to a correct one.
 

Syntax: time.strftime(format[, t])
Parameters : 
t – time in number of seconds to be formatted 
format – This is of string type. i.e. the directives can be embedded in the format string.
Return value: None


There are many directives that can be embedded in the format string, you can refer them here. 
Notes:
 

  • When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
  • The range really is 0 to 61; value 60 is valid in timestamps representing leap seconds and value 61 is supported for historical reasons.
  • When used with the strptime() function, %U and %W are only used in calculations when the day of the week and the year are specified.


Below is the implementation:
 

Python3
# Program To show How can we use different derivatives
# Multiple at a time and single at a time


# importing the srtftime() and gmtime()
# if not used the gm time, time changes
# to the local time

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)

print()

# 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)

print()

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

print()

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

print()

# 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)

print()

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

print()

# % 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)

print()

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

print()

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

print()

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

print()

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

print()

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

Output
Example 1: Tue, 25 Jun 2019 10:09:52 + 1010

Example 2: Tuesday, 06/25/19 June 2019 10:09:52 + 0000

Example 3: Tue Jun 25 10:09:52 2019

Example 4: 20

Example 5: Tuesday, 06/25/19 June 2019, 10:09:52 AM, 
MOTY:06 
DOTY:% j

Example 6:  10:09 

Example 7: Tue, 25 Jun 2019 10:09:52 + 0000

Example 8: 10:09:52 AM, 10:09:52 

Example 9: 10:09:52 AM, 2, 25

Example 10: 10:09:52 AM, 26, 25, 2

Example 11: 06/25/19, 10:09:52, 19, 2019

Example 12: 10:09:52 AM, +0000, UTC

time.strftime() function in Python – FAQs

What is the use of time.strftime in Python?

time.strftime is a powerful function used to format datetime objects (representing date and time) into human-readable strings. It allows you to customize how you want to display the date and time based on your specific needs.

How to get the time string in Python?

Here’s how to use strftime to get a formatted time string:

  • Import the time module:
import time
  • Get the current time:Use time.localtime() to get the current local time as a struct_time object, containing details like year, month, day, hour, minute, etc.
  • Format the time string:Use time.strftime(format, time_object). This function takes two arguments:
    • format: A string containing special placeholders that are replaced with corresponding time components.
    • time_object: The struct_time object (usually obtained from localtime()) that holds the time data.

What is the function Strftime (with a capital S)?

Strftime (with a capital S) is likely a misspelling or shorthand for strftime. It refers to the same function from the time module used for formatting time strings.

How to convert time to string in Python?

You don’t actually convert time to a string in Python; you format an existing datetime object using strftime. This function takes the datetime object and a format string to create a human-readable representation of the time.

How to convert strftime to date in Python?

There’s a misunderstanding here. strftime doesn’t convert a string to a date. It formats a datetime object into a string. To convert a string representation of time back to a datetime object, you can use datetime.strptime() (from the datetime module). This function parses the formatted string according to a specified format code and returns a datetime object.



Next Article

Similar Reads

Article Tags :
Practice Tags :
three90RightbarBannerImg