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