We now have a youtube channel. Subscribe!

Python Time strftime() Method with example



Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be studying about the Python Time strftime() method.

Python strftime() method converts 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, then the current time as returned by the localtime() is used. Format must be a string. An exception ValueError is raised if any field in t is outside the allowed range.

Syntax

The following below is the syntax for the Python Time strftime() method -

time.strftime(format[,  t] )


Parameter Details

  • t - This is the time in number of seconds to be formatted.
  • format - This is the directive which would be used to format given time. The following are the directives which can be embedded on the format string -

Directive

  • %a - abbreviated weekday name
  • %A - full weekday name
  • %b - abbreviated month name
  • %B - full month name
  • %c - preferred date and time representation
  • %C - century number (the year divided by 100, range 00 to 99)
  • %d - day of the month (01 to 31)
  • %D - same as %m%d%y
  • %e - day of the month (1 to 31)
  • %g - like %G, but without the century
  • %G - 4 digit year corresponding to the ISO week number (see %V)
  • %h - same as %b
  • %H - hour, using a 24-hour clock (00 to 23)
  • %I - hour, using a 12-hour clock (01 to 12)
  • %j - day of the year (001 to 366)
  • %m - month (01 to 12)
  • %M - minute
  • %n - newline character
  • %p - either am or pm based on given time value
  • %r - time in a.m. and p.m. notation
  • %R - time in 24 hour notation
  • %S - seconds
  • %t - tab character
  • %T - current time, equal to %H:%M:%S
  • %u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1.
  • %U - week number of the current year, starting with the first Sunday as the first day of the first week.
  • %V - the ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
  • %W - week number of the current year, starting with the first Monday as the first day of the first week
  • %w - day of the week as a decimal, Sunday=0
  • %x - The preferred date representation without the time
  • %X - The preferred time representation without the date
  • %y - year without the century ( range 00 to 99)
  • %Y - year including the century
  • %Z or %z - time zone or name or abbreviation
  • %% - a literal % character

Return Value

It does not return any value.

Example

The following below is a simple example -

#!/usr/bin/python
import time

t = (2020, 7, 16, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))

Output
Below is the output of the above example -

Jul 16 2020 00:03:38


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the Python Time strptime() method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain