Use of the Strptime () method of operating time in Python

Source: Internet
Author: User
Tags current time iso 8601 month name string format in python

This article mainly introduces the use of the Strptime () method of operating time in Python, which is the basic knowledge in Python's introductory study, and the friends who need it can refer to

The Strptime () method analyzes the time string that is represented by format. The return value is the return gmtime () or localtime () of a struct_time.

The format parameter uses the same instruction to use Strftime (), which defaults to the format returned by%Y () that matches the "%a%b%d%h:%m:%s ctime".

ValueError is suspended if the string cannot be parsed in a format, or if it has a parsed excess of data.


The following is the syntax for the Strptime () method:


1 Time.strptime (string[, format])


String-This is the time in which the string format will be parsed according to the given format.

Format-This is the instruction that will be used to resolve the given string.

The following instructions can be embedded in a format string:


%a-Day of the week

%A-Full day of the week

%B-Abbreviated month names

%B-Full month name

%c-Preferred date and time representation

%c-century value (year divided by 100, range from 00 to 99)

%d-day of the month (01?31)


%e-Day of the month (1?31)

%g-similar to%g, but no century

%G-year of 4 digits corresponding to ISO week (see%V)

%h-Similar to%b

%H-hours, using a 24-hour system (00?23)

%I-hours, using a 12-hour system (01?12)

%j-which day of the year (001?366)

%m-month (01?12)


%n-line Feed

%p-based on the given time value am or PM

%r-time in the morning and afternoon symbols: am/pm

%R-time in 24-hour system



%T-current time, equal to%h:%m:%s

%u-workdays are numbers (1 to 7), Monday = 1. Warning: on Sun Solaris, Sunday =1

%u-Number of weeks of the year, first Sunday as the first day of the first week

%V-this year's ISO 8601 weeks (01 to 53), of which the 1th week is at least 4 days of the first week of the year, Monday as the first day of the week

%w-the number of weeks of the year, with the first Monday as the first day of the first week

%w-week for a decimal, Sunday =0

%x-No date representation of time

%x-No date preferred time representation

%y-One year without century (range from 00 to 99)

%Y-this year, including the century

%Z or%Z-time zone or name or abbreviation

Percent%-literal% character

return value

This returns the value of the gmtime () or localtime () returned by Struct_time.


The following example shows the use of the Strptime () method.


1 2 3 4 5 6 7 8 9 #!/usr/bin/python Import Time Struct_time = Time.strptime ("Nov", "%d%b%y") print "returned tuple:%s"% struct_ Time we run above, it produces following result:returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335,-1)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.