Python time processing method

Source: Internet
Author: User
Tags timedelta
In practice, if you encounter a time processing problem, you need to compare Sep06, 201419: 30 (UTC time) with the current time sooner or later. we know this 2014-09-0619: in practice, if a time processing problem occurs for a 30-Period, you need to compare the Sep 06,201 4 (UTC time) with the current time to know the operation of the time in the format. Therefore, when processing, you just want

First, the sep format time is converted into the time format in the following format, and no related function is found. Therefore, I wrote a program and only recorded it for query.

The code is as follows:

#-*-Coding: UTF-8-*-from datetime import datefrom datetime import datetimefrom datetime import timedelta ### if the current time is returned, you can simply write it as # time. localtime () ### the returned UTC time ## time. gmtime () # lt = time. localtime () # tm = time. gmtime () # ft = time. strftime ('% Y-% m-% d % H-% M', lt) # ft2 = time. strftime ('% Y-% m-% d % H: % M', tm) # print ft, ft2 # print' -------------------------------------------------------- '# now = datetime. datetime. now () # now = now. replace (day = 1) # print now. time () # mytime = ['2017-09-06 20:19 '] # mytime2 = '2017-10-09' # str = "". join (mytime) # print str # retime = datetime. strptime (str, '% Y-% m-% d % H: % M') # print retime # retime = retime + timedelta (hours = 8) # print retime # tdtime = datetime. now () # print tdtime # if retime <= tdtime-timedelta (days = 7): # print "too early" ### Sep 06,201 4 monthdic = {'Jan ': '01', 'Feb': '02', 'Mar ': '03', 'Apr': '04', 'may': '05 ', 'Jun ': '06 ', 'Jul': '07', 'Aug': '08 ', 'Sep': '09', 'Oct': '10', 'Nov ': '11', 'Dec ': '12'} def time_format (timestr): timestr = timestr. replace (',', '') # print timestr timelist = timestr. split () # print timelist mon = "". join (timelist [0]) # print mon timelist [0] = monthdic [mon] # print timelist mytime = "". join (timelist [2]) + '-' + "". join (timelist [0]) + '-' + "". join (timelist [1]) + ''+ "". join (timelist [3]) return mytimeif name = 'main': timestr = 'sep 06,201 4 'str = time_format (timestr) print str mytime = datetime. strptime (str, '% Y-% m-% d % H: % M') print mytime = mytime + timedelta (hours = 8) print mytime tdtime = datetime. now () print tdtime if mytime <= tdtime-timedelta (days = 7): print "too early"

The above is the details of the time processing method using python. For more information, see other related articles in the first PHP community!

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.