[英]How to compare datetime with current date?
I have the following with me我有以下物品
>>> import datetime
>>>
>>> some_date = datetime.datetime.strptime('2017-07-03T20:35:45.000Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> some_date
datetime.datetime(2017, 7, 3, 20, 35, 45)
>>> datetime.datetime.now()
datetime.datetime(2017, 8, 21, 22, 4, 20, 215391)
>>>
I wanted to find out whether some_date
is N days older than today or not?我想知道
some_date
是否比今天大 N 天? Like lets' take N as 2. Then I wanted to find whether some_date came two days ago or not.比如让我们把 N 取为 2。然后我想知道 some_date 是否在两天前出现。 How should I subtract both?
我应该如何减去两者? Would this work?
这行得通吗? https://stackoverflow.com/a/441152/3834059
https://stackoverflow.com/a/441152/3834059
you can check it like this你可以这样检查
you dont need to str time你不需要时间
import datetime
if my_time == datetime.timedelta(days=2):
print("my time is for 2 days ago")
or another examples for past times或过去的其他例子
import datetime
if my_time >= datetime.datetime.now() - datetime.timedelta(days=1):
print("my time is in past")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.