How do i compare two datetime values in python?

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.

How do i compare two datetime values in python?

codeforester

35.7k16 gold badges101 silver badges126 bronze badges

asked Nov 15, 2011 at 19:58

Steven MatthewsSteven Matthews

8,91741 gold badges116 silver badges213 bronze badges

7

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)

How do i compare two datetime values in python?

Thomas Decaux

20.5k2 gold badges104 silver badges112 bronze badges

answered Nov 15, 2011 at 20:02

9

Use time

Let's say you have the initial dates as strings like these:

date1 = "31/12/2015"
date2 = "01/01/2016"

You can do the following:

newdate1 = time.strptime(date1, "%d/%m/%Y")
newdate2 = time.strptime(date2, "%d/%m/%Y")

to convert them to python's date format. Then, the comparison is obvious:

  • newdate1 > newdate2 will return False
  • newdate1 < newdate2 will return True

How do i compare two datetime values in python?

wjandrea

24.6k8 gold badges53 silver badges73 bronze badges

answered Jul 21, 2015 at 16:41

1

datetime.date(2011, 1, 1) < datetime.date(2011, 1, 2) will return True.

datetime.date(2011, 1, 1) - datetime.date(2011, 1, 2) will return datetime.timedelta(-1).

datetime.date(2011, 1, 1) + datetime.date(2011, 1, 2) will return datetime.timedelta(1).

see the docs.

answered Nov 15, 2011 at 20:03

Daniel NillDaniel Nill

5,32110 gold badges44 silver badges62 bronze badges

1

Other answers using datetime and comparisons also work for time only, without a date.

For example, to check if right now it is more or less than 8:00 a.m., we can use:

import datetime

eight_am = datetime.time( 8,0,0 ) # Time, without a date

And later compare with:

datetime.datetime.now().time() > eight_am  

which will return True

answered Jan 12, 2019 at 21:13

How do i compare two datetime values in python?

LuisLuis

3,1694 gold badges32 silver badges55 bronze badges

With python as the easiest language available it is pretty easy to compare dates in python the python operators <, > and == fit wonderfully with datetime objects. each of them has their own meaning in python:

  • < means the date is earlier than the first
  • > means the date comes later
  • == means the date is same as the first So, for your case:
import datetime

date = datetime.datetime(2000, 1, 1) # Replace with whatever you want
now = datetime.datetime.now() # You can even find the current date and time using this expression

if date < now:
    print('past')
elif date > now:
    print('future')
else:
    print('present')
# This would print "past"

rurban

3,87823 silver badges27 bronze badges

answered May 22, 2021 at 10:26

1

For calculating days in two dates difference, can be done like below:

import datetime
import math

issuedate = datetime(2019,5,9)   #calculate the issue datetime
current_date = datetime.datetime.now() #calculate the current datetime
diff_date = current_date - issuedate #//calculate the date difference with time also
amount = fine  #you want change

if diff_date.total_seconds() > 0.0:   #its matching your condition
    days = math.ceil(diff_date.total_seconds()/86400)  #calculate days (in 
    one day 86400 seconds)
    deductable_amount = round(amount,2)*days #calclulated fine for all days

Becuase if one second is more with the due date then we have to charge

answered May 23, 2019 at 12:41

2

How do I compare two datetime values?

Compare() method in C# is used for comparison of two DateTime instances..
<0 − If date1 is earlier than date2..
0 − If date1 is the same as date2..
>0 − If date1 is later than date2..

Can we compare two timestamps in Python?

And, it is required to compare timestamps to know the latest entry, entries between two timestamps, the oldest entry, etc. for various tasks. Comparison between pandas timestamp objects is carried out using simple comparison operators: >, <,==,< = , >=. The difference can be calculated using a simple '–' operator.

Can we compare date with datetime?

When you have two datetime objects, the date and time one of them represent could be earlier or latest than that of other, or equal. To compare datetime objects, you can use comparison operators like greater than, less than or equal to. Like any other comparison operation, a boolean value is returned.

How do I find the difference between two times in Python?

For example, the %H:%M:%S format codes are for hours, minutes, and seconds. To get the difference between two-time, subtract time1 from time2.