只比较今天的日期,没有 hours/min/sec
Compare only today's date without hours/min/sec
我想将今天的时间与另一个没有 hour/min 或秒的新时间进行比较。忽略小时和分钟...
换句话说,如果两天都相同,无论小时或分钟,都可以(额外)否则不好(太旧)...我试过了,但即使是同一天,仍然会继续与hour/min 和秒。我该怎么做?
import datetime
currentdate = datetime.datetime.now()
print(currentdate)
newsdate = "Oct-26-2021"
newsdate = datetime.datetime.strptime(newsdate, "%b-%d-%Y")
print(newsdate)
if currentdate == newsdate:
print("Extra")
else:
print("Too old")`
比较仅使用日期时间的 date 组件,可通过 .date()
属性访问:
import datetime
currentdate = datetime.datetime.now()
print(currentdate)
newsdate = "Oct-26-2021"
newsdate = datetime.datetime.strptime(newsdate, "%b-%d-%Y")
print(newsdate)
if currentdate.date() == newsdate.date():
print("Extra")
else:
print("Too old")
以上内容也可以改写如下(可能有助于其他人的可读性):
from datetime import date, datetime
currentdate: date = date.today()
print(currentdate)
date_string = "Oct-26-2021"
newsdate: date = datetime.strptime(date_string, "%b-%d-%Y").date()
print(newsdate)
if currentdate == newsdate:
print("Extra")
else:
print("Too old")
我想将今天的时间与另一个没有 hour/min 或秒的新时间进行比较。忽略小时和分钟...
换句话说,如果两天都相同,无论小时或分钟,都可以(额外)否则不好(太旧)...我试过了,但即使是同一天,仍然会继续与hour/min 和秒。我该怎么做?
import datetime
currentdate = datetime.datetime.now()
print(currentdate)
newsdate = "Oct-26-2021"
newsdate = datetime.datetime.strptime(newsdate, "%b-%d-%Y")
print(newsdate)
if currentdate == newsdate:
print("Extra")
else:
print("Too old")`
比较仅使用日期时间的 date 组件,可通过 .date()
属性访问:
import datetime
currentdate = datetime.datetime.now()
print(currentdate)
newsdate = "Oct-26-2021"
newsdate = datetime.datetime.strptime(newsdate, "%b-%d-%Y")
print(newsdate)
if currentdate.date() == newsdate.date():
print("Extra")
else:
print("Too old")
以上内容也可以改写如下(可能有助于其他人的可读性):
from datetime import date, datetime
currentdate: date = date.today()
print(currentdate)
date_string = "Oct-26-2021"
newsdate: date = datetime.strptime(date_string, "%b-%d-%Y").date()
print(newsdate)
if currentdate == newsdate:
print("Extra")
else:
print("Too old")