# 给时间字段一个默认值,自己构造个方法:设置为当日的8点
def _default_last_borrow_date(self):
now = datetime.datetime.now()
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
lastToday = zeroToday + datetime.timedelta(hours=-8, minutes=00, seconds=00)
return lastToday
date_range_end = fields.Datetime(string='结束时间',default=_default_last_borrow_dates,required=True)
# python的时间玩法
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 获取今天零点
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
# 获取23:59:59
lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
# 获取前一天的当前时间
yesterdayNow = now - datetime.timedelta(hours=23, minutes=59, seconds=59)
# 获取明天的当前时间
tomorrowNow = now + datetime.timedelta(hours=23, minutes=59, seconds=59)
print('时间差', datetime.timedelta(hours=23, minutes=59, seconds=59))
print('当前时间', now)
print('今天零点', zeroToday)
print('获取23:59:59', lastToday)
print('昨天当前时间', yesterdayNow)
print('明天当前时间', tomorrowNow)
输出:
时间差 23:59:59
当前时间 2019-08-19 10:16:32.510070
今天零点 2019-08-19 00:00:00
获取23:59:59 2019-08-19 23:59:59
昨天当前时间 2019-08-18 10:16:33.510070
明天当前时间 2019-08-20 10:16:31.510070