datetime

日期格式化

import datetime
# 日期格式化
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')
# 字符串转日期
datestr = '2016-03-15 19:38:17'
datetime.datetime.strptime(datestr, '%Y-%m-%d %H:%M:%S')
datetime.datetime(2016, 3, 15, 19, 38, 17)

获取一天

import datetime
# 获取一天的开始与结尾
def get_start_of_today():
"""获取今天的开始时间."""
return datetime.datetime.combine(datetime.date.today(), datetime.time.min)
def get_end_of_today():
"""获取今天的结束时间."""
return datetime.datetime.combine(datetime.date.today(), datetime.time.max)