Python Time processing

Source: Internet
Author: User


In practice, there is a time to deal with the problem, need to be Sep, 19:30 (UTC time) and the current time compared sooner or later, know this 2014-09-06 19:30 format time operation. So, when you're dealing with it, you want to

The SEP format time is converted to the time of the following format, no correlation function is found, so he simply wrote a program, only in this, in case of query

The code is as follows:

#-*-Coding:utf-8-*-from datetime import datefrom datetime import datetimefrom datetime import timedelta# # #如果是返回当前时 Time.localtime () # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # time.gmtime () # lt = time.localtime () # TM = Time.gmtime () # = Time.strftim E ('%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# print now.time () # Mytim e = [' 2014-09-06 20:19 ']# #mytime2 = ' 2014-10-09 14:32 ' # 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 TD time# if Retime <= tdtime-timedelta (days = 7): # print "Too early" # #Sep, 19:30monthdic = {' Jan ': ' 01 ', ' Feb ': ' ' Mar ': ' The ' ', ' Apr ': ' A ', ' may ': ' ', ' June ': ' ', ' Jul ': ' ', ' ' ' ' ' ' ' ', ' Sep ': ' The ', ' ' Oct ': ' 11 ', ' Dec ': '}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__ ': Time str = ' Sep, 19:30 ' str = time_format (timestr) print str mytime = datetime.strptime (str, '%y-%m-%d %h:%m ') print mytime mytime = MyTime +timedelta (hours = 8) Print MyTime tdtime = DateTime.Now () print Tdtime if MyTime <= tdtime-timedelta (days = 7): print "too early"

Python Time processing

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.