Python Tutorial

Showing posts with label date time comparison. Show all posts
Showing posts with label date time comparison. Show all posts

Wednesday, February 20, 2013

date compare - code snippet

import datetime

a = datetime.date(2012,1,31) # YYYY, MM, DD
b = datetime.date(2009,10,31) # YYYY, MM, DD

print a == b
print a > b
print a < b


Output:
False
True
False

Thursday, July 21, 2011

Date time

Python date time comparison:
For python date time comparison you can directly compare between two date and also make some mathematical operation
import datetime
import time
before=datetime.datetime.now()
print "before "+str(before)
time.sleep(5)
after=datetime.datetime.now()
print "after "+str(after)
difference=after-before
print "difference "+str(difference)



before 2011-07-21 15:38:13.330000
after 2011-07-21 15:38:18.347000
difference 0:00:05.017000