为什么80%的码农都做不了架构师?>>>
一、一个日期字符串d_str想要计算向前7天的操作如下:
输入str
>>> from datetime import *
>>> d_str='2015-07-31'
>>> d_str
'2015-07-31'
str转换成datetime
>>> dt = datetime.strptime(d_str, '%Y-%m-%d')
>>> dt
datetime.datetime(2015, 7, 31, 0, 0)
向前偏移7天
>>> dt_p7 = dt - datetime.timedelta(days=7)
>>> dt_p7
datetime.datetime(2015, 7, 24, 0, 0)
datetime转str
>>> dt_p7.strftime('%Y-%m-%d')
'2015-07-24'
二、其他函数:
1、now()与today():看起来效果差不多
>>> datetime.datetime.now()
datetime.datetime(2015, 7, 31, 10, 5, 50, 419801)
>>> datetime.datetime.today()
datetime.datetime(2015, 7, 31, 10, 6, 3, 820146)