Python time processing and python Processing
In practice, if you encounter a time processing problem, you need to compare Sep 06,201 4 (UTC time) with the current time sooner or later 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 19: 30 monthdic = {'Jan ': '01', 'feb': '02', 'mar': '03', 'apr': '04 ', 'may ': '05 ', 'jun': '06', 'jul': '07', 'aug': '08', 'sept': '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"
Python Time Processing
Do not use that
If you only need to find files larger than 4 days, you can do this:
Import datetime
Ct = OS. stat ("file path"). st_ctime
Today = datetime. date. today ()
Print today-datetime. date. fromtimestamp (ct). days
# This is the number of days from the "file path" file today. I believe you can solve the problem.
Python datetime Processing Time
Determine the size in advance:
Timedelta = time1-time2 if time1> time2 else time2-time1
Reference: www.zlovezl.cn