Python time strptime() method
Description
The Python time strptime() function parses a time string into a time tuple according to the specified format.
Syntax
strptime() method syntax:
time.strptime(string[, format])
Parameters
- string -- the time string.
- format -- the format string.
Return Value
Returns a struct_time object.
Description
Date and time formatting symbols in Python:
- %y two-digit year (00-99)
- %Y four-digit year (0000-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 number of minutes (00-59)
- %S seconds (00-59)
- %a abbreviated local weekday name
- %A full local weekday name
- %b abbreviated local month name
- %B full local month name
- %c local 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), with Sunday as 0, Monday as 1, and so on.
- %W week number of the year (00-53), Monday as the first day of the week
- %x local date representation
- %X local time representation
- %Z name of the current time zone
- %% the % character itself
Example
The following example shows 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
"
)
"
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)