Datetime.datetime.strptime: A universal date format conversion

Source: Internet
Author: User
Environment:
distro:os X 10.12.3
python interpreter: 3.6.1


recently wrote a news aggregation app, in which the server uses Python to crawl RSS data from major news sites. One of the problems encountered is the unification of date formats:
Sina News's RSS date format is this:
<pubdate>sun, APR 2017 05:15:05 gmt</pubdate>
Baidu News of the date format of the RSS is this:
<pubdate><! [cdata[2017-04-23t05:27:20.000z]]></pubdate>
I was hoping to unify the date with Gus for 2017-04-23 11:11:11. I found this interface.
Datetime.datetime.strptime ()
The question is how to use it, see the code directly to understand:
Import datetime #python3解释器自带datetime模块
newstime= ' Sun, APR 2017 05:15:05 GMT '
gmt_format = '%a,%d%b%Y: %s GMT '
newstime=datetime.datetime.strptime (newstime,gmt_format)
print (newstime) #2017-04-23 05:15:05
We succeeded in getting the result, and we can see that the key to the problem is the second parameter of the interface: Gmt_format: As long as the Gmt_format match the original date format
, GMT represents GMT, if you want to convert to Beijing time, plus 8 hours:
Newstime=datetime.datetime.strptime (Newstime,bai_format) +date.timedelta (hours=8)
Baidu news site's RSS time format left to the reader's own conversion.

Attention:

The placeholders for apr,sept, such as month, are%b, and 03,04 these month digits are%m

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: info-contact@alibabacloud.com 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.