Get current date/time and compare with other date

Question:

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’

Asked By: pufAmuf

||

Answers:

Inside datetime module, a class is named datetime aswell which you probably know since you did it right in the rest of the code.

Your third line should be:

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

And insted of two datetimes there is only one. That line raises an error.
Alternatively, you can just import the whole class:

from datetime import datetime

And there won’t be any need to specify datetime two times which is a tad easier.

Edit: As Two-Bit Alchemist pointed out (which I honestly haven’t noticed) is that you’re comparing dates by a string which isn’t going to work a good practice. Take a look at various snippets in this question about comparing dates in Python without converting them to strings.

Answered By: Mirza

I have found out this excellent module that make date manipulation so simple:

import arrow

n = arrow.utcnow()
expected = arrow.get("9/8/2015 4:00", "D/M/YYYY H:m")

if n > expected:
    print("Date Missed.")
else:
    print("Date not missed.")
Answered By: Ali SAID OMAR

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
Answered By: Kevin

One thing that string can’t be compared to Datetime.
And Just format both in

%Y-%m-%d

so that we can compare both…
I have corrected your code and tried it. it’s working.

and If you want to add time in it use this in both formating

%Y-%m-%d %H:%M:%S

import datetime

CurrentDate = datetime.datetime.now().strftime('%Y-%m-%d')
print(CurrentDate)

ExpectedDate = "9/8/2015"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y").strftime('%Y-%m-%d')
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")
Answered By: Epic Ansh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.