1
leavic 2015-10-26 23:17:21 +08:00
>>> import datetime
>>> datetime.datetime.now() |
2
woohaha 2015-10-26 23:19:55 +08:00
from datetime import datetime
datetime_from_db='2015-10-26 00:00:00' datetime_of_datetime_from_db=datetime.strptime(datetime_from_db,'%Y-%m-%d %H:%M:%S') delta_time=datetime.now()-datetime_of_datetime_from_db if delta_time.days<0: print('expired') else: print('valid') |
3
DeanThompson 2015-10-26 23:39:57 +08:00 1
注意一下时区是否相同,如果相同就简单了:
```python import datetime def is_expired(dt): if isinstance(dt, str): dt = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') return datetime.datetime.now() > dt ``` >>> is_expired('2015-10-26 00:00:00') True >>> is_expired('2015-11-26 00:00:00') False |