使用Python比较两个日期

时间:2018-12-10 18:02:02

标签: python

import time

date1 = "31/12/2015"
date2 = "01/01/2019"


newdate1 = time.strptime(date1, "%d/%m/%Y")
newdate2 = time.strptime(date2, "%d/%m/%Y")


if newdate1 > newdate2:
    print "Certificate got Expired on ",date1
else:
    print "Certificate will be expired on ",date2

上面的代码在任何在线python编译器中都可以正常运行,但是每当我在服务器上运行它时,我都会收到错误消息, “ AttributeError:类'org.python.modules.time'没有属性'strptime'

请注意,我不允许使用datetime模块或其功能,因为那样的话我会收到新的错误,例如“ ImportError:没有名为datetime的模块”

1 个答案:

答案 0 :(得分:0)

如果您在日期字符串上使用 YYYY-MM-DD (年-月-日)格式,则可以按字典顺序将它们作为字符串进行比较。字符的字典顺序将与时间顺序一致。您甚至不需要为此导入模块。

假设您的日期字符串长度相同(必要时用前导零填充),并且使用相同的定界符。

相关问题