Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two dates?

How would I compare two dates to see which is later, using Python?

For example, I want to check if the current date is past the last date in this list I am creating, of holiday dates, so that it will send an email automatically, telling the admin to update the holiday.txt file.

like image 944
Steven Matthews Avatar asked Nov 15 '11 19:11

Steven Matthews


1 Answers

Use the datetime method and the operator < and its kin.

>>> from datetime import datetime, timedelta >>> past = datetime.now() - timedelta(days=1) >>> present = datetime.now() >>> past < present True >>> datetime(3000, 1, 1) < present False >>> present - datetime(2000, 4, 4) datetime.timedelta(4242, 75703, 762105) 
like image 128
Fred Foo Avatar answered Sep 22 '22 07:09

Fred Foo