Change language

time.strftime () function in Python

|

time.strftime (format [, t]) converts tuprl or struct_time representing the time returned by gmtime () or localtime () to the string specified by the format argument. 
If t is not specified, the current time returned by localtime () is used. The format must be string.  ValueError is raised if any field in t is out of range.

Notes:
0 is a valid argument for any position in temporary tuple; if it is usually illegal, the value is forced to be correct.

Syntax: time.srtftime (format [, t])

Parameters:
t - time in number of seconds to be formatted
format - This is of string type. ie the directives can be embedded in the format string.

Return value: None

There are many directives that can be embedded in a string format, you can link to them here .

Notes :

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

Below is the implementation:

# Program To show how we can use different derivatives
# Several at the same time and one at a time

 

 
# srtftime () and gmtime () imports
# if time is not used um, time changes
# local time

 

from time import gmtime, strftime

  
# using a simple time format

s = strftime ( "% a,% d% b% Y% H:% M:% S + 1010 " , gmtime ())

print ( " Example 1: " , s)

 

print ()

 
# only in full names
# and view

s = strftime ( "% A,% D% B% Y% H:% M:% S + 0000 " , gmtime ())

print ( " Example 2: " , s)

 

print ()

 
# this will show you your preferred date and time format

s = strftime ( "% c " )

print ( "Example 3:" , s)

 

print ()

 
# this will tell about centuries

s = strftime ( "% C" )

print ( "Example 4:" , s)

 

print ()

 
# MOTY: month of the year
# DOTY: day of the year
# Simple view
#% n - newline

s = strftime ( "% A,% D % B% Y,% r,% nMOTY:% m% nDOTY:% j " )

print ( "Example 5:" , s)

 

print ()

 
# % R - time in 24-hour recording

s = strftime ( "% R" )

print ( "Example 6:" , s)

 

print ()

  
#% H - hour, using 24- hour hours (00 to 23) in Examples 1, 2, 3
#% I - hour, using 12 hour hours (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 and% U (see the difference)

s = strftime ( "% r,% u,% U" )

print ( "Example 9:" , s)

 

print ()

 
# using% V,% W,% w

s = strftime ( "% r,% V,% W,% w" )

print ( "Example 10:" , s)

 

print ()

 
# using% x,% X,% y,% Y

s = strftime ( "% x,% X,% y,% Y " )

print ( "Example 11:" , s)

 

print ()

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method