可以使用datetime模块来比较当前时间与指定日期。下面是一个示例代码:
import datetime
# 获取当前时间
current_time = datetime.datetime.now()
# 将指定日期字符串转换为datetime对象
specified_date = datetime.datetime.strptime("2019-01-01 00:00:00.000", "%Y-%m-%d %H:%M:%S.%f")
# 比较当前时间与指定日期
if current_time > specified_date:
print("当前时间晚于指定日期")
elif current_time < specified_date:
print("当前时间早于指定日期")
else:
print("当前时间与指定日期相等")
在上面的代码中,我们首先使用datetime.datetime.now()
函数获取当前时间,然后使用datetime.datetime.strptime()
函数将指定日期字符串转换为datetime对象。最后,我们使用比较运算符来比较当前时间与指定日期,并根据比较结果输出相应的信息。