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 times = 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 representations = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime())print("Example 2:", s)Â
print()Â
# this will show you the preferred date time formats = strftime("%c")print("Example 3:", s)Â
print()Â
# this will tell about the centuriess = strftime("%C")print("Example 4:", s)Â
print()Â
# MOTY: month of the year# DOTY: Day of the year# Simple representation# % n - new lines = strftime("%A, %D %B %Y, %r, %nMOTY:%m %nDOTY:% j")print("Example 5:", s)Â
print()Â
# % R - time in 24 hour notations = 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:% Ss = 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, % ws = strftime("%r, %V, %W, %w")print("Example 10:", s)Â
print()Â
# use of % x, % X, % y, % Ys = strftime("%x, %X, %y, %Y")print("Example 11:", s)Â
print()Â
# use of % Z, % zs = strftime("%r, %z, %Z")print("Example 12:", s) |
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
Â
