Python time strptime() Method


Description

The Python time strptime() function parses a time string into a time tuple according to the specified format.

Syntax

Syntax of the strptime() method:

time.strptime(string[, format])

Parameters

  • string -- Time string.
  • format -- Format string.

Return Value

Returns a struct_time object.

Explanation

Time and date formatting symbols in Python:

  • %y Two-digit year representation (00-99)
  • %Y Four-digit year representation (000-9999)
  • %m Month (01-12)
  • %d Day of the month (0-31)
  • %H Hour in 24-hour format (0-23)
  • %I Hour in 12-hour format (01-12)
  • %M Minutes (00-59)
  • %S Seconds (00-59)
  • %a Local abbreviated weekday name
  • %A Local full weekday name
  • %b Local abbreviated month name
  • %B Local full month name
  • %c Local appropriate date and time representation
  • %j Day of the year (001-366)
  • %p Local equivalent of A.M. or P.M.
  • %U Week number of the year (00-53), Sunday as the first day of the week
  • %w Weekday (0-6), Sunday is 0, Monday is 1, and so on.
  • %W Week number of the year (00-53), Monday as the first day of the week
  • %x Local appropriate date representation
  • %X Local appropriate time representation
  • %Z Name of the current time zone
  • %% The % sign itself

Example

The following example demonstrates how to use the strptime() function:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- import time struct_time = time.strptime("30 Nov 00", "%d %b %y") print "Returned tuple: %s" % struct_time

The output of the above example is:

返回的元组: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) 
Other Extensions