Latest web development tutorials

Python time strftime () method

description

Python time strftime () function takes a time tuple and returns a string representation of the local time in a readable format determined by the parameter format.

grammar

strftime () method syntax:

time.strftime(format[, t])

parameter

  • format - format string.
  • t - t is an optional parameter struct_time object.

return value

Returns a string representation of local time to read.

Explanation

python, date and time formatting symbols:

  • % Y denote two-digit year (00-99)
  • % Y represents a four-digit year (000-9999)
  • % M month (01-12)
  • Within% d month of the day (0-31)
  • % H 24-hour clock hours (0-23)
  • % I 12 hour clock hours (01-12)
  • % M number (00 = 59) minutes
  • % S seconds (00-59)
  • % A week simplify local name
  • Local% A full weekday name
  • % B local simplify month name
  • % B full month name locally
  • % C local date corresponding representation and time representation
  • % J day of the year (001-366)
  • % P AM or PM local equivalent character
  • The% U several times a year (00-53) for the week beginning Sunday week
  • % W week (0-6), Sunday is the beginning of the week
  • % W of the year number (00-53) for the week beginning Monday week
  • % X corresponding local date representation
  • % X corresponding local time representation
  • Name% Z current time zone
  • %%% Number itself

Examples

The following example shows strftime () function to use:

#!/usr/bin/python
import time

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

The above example output is:

Feb 17 2009 09:03:38