Issue
I'm trying to compare 2 dates in python, when I print the dates they are the same but the comparison fails.
import datetime
today = datetime.date.today()
print today
print '2019-04-30'
d1 = today
d2 = '2019-04-30'
if d1 == d2:
print 'match'
else:
print 'nomatch'
Totally confused why dates that look the same but the comparison fails.
Solution
They're not the same. You can test it out by checking their types
type(d1)
<class 'datetime.date'>
type(d2)
<class 'str'>
Printing d1 gives you the same string because datetime objects have a __repr__
method that returns string.
Answered By - Adarsh Chavakula
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.