strftime和strptime 的区别
时间的处理
import datetime
>>> print datetime.datetime.now()
2017-07-15 15:01:24.619000
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2017-07-15 15:01:35
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2017-07-15 15:01
>>> print datetime.datetime.now().strftime("%Y%m%d")
20170715
>>> print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y%m-%d %H:%M:%S")
2017-07-16 15:12:42
>>> print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
2017-07-15 16:10:43
>>> print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")
2017-07-15 15:12:56
>>> import datetime
>>> d1 = datetime.datetime(2005, 2, 16)
>>> d2 = datetime.datetime(2004, 12, 31)
>>> (d1 - d2).days
47
>>>import datetime
>>>starttime = datetime.datetime.now()
>>>endtime = datetime.datetime.now()
>>>print (endtime - starttime).seconds
求当前分钟及下一分钟
from datatime import datetime,date,timedelta
t = datetime.strptime(str(datetime.now()).split('.')[0], '%Y-%m-%d %H:%M:%S')
t = datetime.strptime(str('2021-08-07 23:59:00').split('.')[0], '%Y-%m-%d %H:%M:%S')
t = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
t_date = date(*map(int, date.strftime(t, "%Y-%m-%d").split('-')))
start_time = datetime.strptime(datetime.strftime(t, '%Y-%m-%d %H:%M') + ':00','%Y-%m-%d %H:%M:%S')
end_tim= datetime.strptime(datetime.strftime(start_time,'%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')+timedelta(minutes=1)
t = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
t_date = date(*map(int, date.strftime(t, "%Y-%m-%d").split('-')))
end_time = datetime.strptime(datetime.strftime(t, '%Y-%m-%d %H:%M') + ':00','%Y-%m-%d %H:%M:%S')
start_time = datetime.strptime(datetime.strftime(end_time,'%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')+timedelta(minutes=-1)
t = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
t_date = date(*map(int, date.strftime(t, "%Y-%m-%d").split('-')))
end_time = datetime.strptime(datetime.strftime(t, '%Y-%m-%d %H:%M') + ':00','%Y-%m-%d %H:%M:%S')
start_time = datetime.strptime(datetime.strftime(end_time,'%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')+timedelta(minutes=-1)
end_time2 = datetime.strptime(datetime.strftime(start_time, '%Y-%m-%d %H:%M') + ":59", '%Y-%m-%d %H:%M:%S')
|