Get the current date / time and compare with another date

advertisements

I'm trying to get the current time, and compare it with a date taken from a string. This is the code I have:

import datetime

CurrentDate = str(datetime.datetime.now())
CurrentDate = datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M")
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")

But this is the error I get.

CurrentDate = datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M") AttributeError: 'module' object has no attribute 'strptime'


There's not much point in converting datetime.datetime.now() into a string, just so you can convert it right back to a datetime. Just leave it as-is.

import datetime

CurrentDate = datetime.datetime.now()
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")

Result:

2015-09-09 12:25:00.983745
2015-08-09 04:00:00
Date missed