如何获得时间17:00:00今天或昨天? [英] How to get time 17:00:00 today or yesterday?
本文介绍了如何获得时间17:00:00今天或昨天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果今天17:00已经通过,那么应该是今天的日期,否则 - 昨天。
今天我得到的:
If 17:00:00 today is already passed, then it should be today's date, otherwise - yesterday's. Today's time I get with:
test = datetime.datetime.now().replace(hour=17,minute=0,second=0,microsecond=0)
但是我不想有未来时间。如何解决?
But I don't want to have future time. How can I fix it?
推荐答案
您可以检查当前时间是否小于17:00,如果是,则减1从生成的时间对象的一天:
You could check if the current time is less than 17:00, if so, substract one day from the generated time object:
test = datetime.datetime.now().replace(hour=17,minute=0,second=0,microsecond=0)
if datetime.datetime.now() < test:
test = test - datetime.timedelta(days=1)
这篇关于如何获得时间17:00:00今天或昨天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文