以下是一个比较输入日期与当前日期的问题的解决方法的代码示例:
from datetime import datetime
def compare_dates(input_date):
current_date = datetime.now().date()
input_date = datetime.strptime(input_date, '%Y-%m-%d').date()
if input_date < current_date:
print("输入日期在当前日期之前")
elif input_date == current_date:
print("输入日期与当前日期相同")
else:
print("输入日期在当前日期之后")
# 示例调用
input_date = input("请输入日期(YYYY-MM-DD):")
compare_dates(input_date)
在这个示例中,我们使用datetime
模块来处理日期。首先,我们获取当前的日期current_date
,然后将输入的日期字符串input_date
转换为datetime
对象,并提取日期部分。然后,我们使用if-elif-else
语句来比较input_date
和current_date
,并打印相应的结果。
请注意,这个示例假设输入的日期格式为YYYY-MM-DD
。您可以根据实际需求修改日期格式的字符串,以适应不同的日期格式。