Latest web development tutorials

Python time strptime () method

description

Python time strptime () function according to the specified format to parse a time string as a time tuple.

grammar

strptime () method syntax:

time.strptime(string[, format])

parameter

  • string - time string.
  • format - format string.

return value

Back struct_time object.

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 strptime () function to use:

#!/usr/bin/python
import time

struct_time = time.strptime("30 Nov 00", "%d %b %y")
print "returned tuple: %s " % struct_time

The above example output is:

returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335, -1)