1. 常用操作
from datetime import datetime
aa = datetime.now()
print(aa)
print(str(aa))
bb = datetime.strptime(str(aa),"%Y-%m-%d %H:%M:%S.%f")
2. 字符串格式转换datetime日期格式
aa = datetime.datetime.strptime('2017-10-11', '%Y-%m-%d')
3. datetime日期格式转换成字符串
today = datetime.datetime.now()
now = datetime.datetime.strftime(today, '%Y-%m-%d')
4. 计算两个日期相差的天数、秒数
from datetime import datetime
aa = datetime.now()
time.sleep(100)
bb = datetime.now()
(bb-aa).days
(bb-aa).seconds
(bb-aa).total_seconds
5. 多加一天、一小时、一分钟
a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
b = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')
a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
b = (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')
a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
b = (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')