1

I have a simple question today. I want to get the difference between the 2 dates and check if the difference is around ~5 minutes.

And I found a problem with getting the difference, to check.

I compared the same date with one that had a few minutes difference, and it printed a difference of -1 day? That doesn't make sense to me.

Test it yourself, if you want.

Compared Dates:

Date1: 2021-05-15 00:38:57.244000
Date2: 2021-05-15 02:40:42.245693

The printed difference was:

Diff: -1 day, 21:58:14.998307

So why is it at -1 day? Shouldn't the difference be just ~2 hours? And what's the best way to get the difference between the 2 dates? As I said, I want to check if the difference is smaller (or equal to) 5 minutes. How is that possible if the dates can be every time different?

Important info: The dates are always different because I check account creation dates.

I used this code to make the difference:

diff = date1 - date2
  • what if you do `date2 - date1`? – hjpotter92 May 15 '21 at 00:50
  • The dates are always different because i check account creation dates. –  May 15 '21 at 00:51
  • date2 is later than date1, so `date1 - date2` is negative. Don't you want `date2 - date1` instead? – John Gordon May 15 '21 at 00:59
  • @JohnGordon Because I compare account creation dates, the date1 or date2 can be every time different. It can be a date from 2018 or 2021, and date2 too. I just want a method to check if the difference between the both dates is smaller (or equal to) 5 minutes. –  May 15 '21 at 01:03
  • Compare the dates to see which is smaller, then subtract accordingly. – John Gordon May 15 '21 at 01:09

3 Answers3

1

This problem can be solved by ordering the two date objects so that the diff comparison always returns a positive number. Then you can use a timedelta object to check if the diff is less than 5 minutes.

from datetime import datetime, timedelta

# Define dates as datetime objects
Date1 = datetime.fromisoformat("2021-05-15 00:38:57.244000")
Date2 = datetime.fromisoformat("2021-05-15 02:40:42.245693")

# Define 5 minutes as a timedelta object
five_minute_duration = timedelta(minutes=5)

Compare the two dates by subtracting the older date from the newer date. The result will be a timedelta object.

diff = Date2 - Date1

If you're not sure which date object is the older and younger, you could sort them. This will make the comparison stable and prevent the unexpected negative timedelta.

dates_sorted = sorted([Date2, Date1])
diff = dates_sorted[1] - dates_sorted[0]

Difference in hours. In this the example, the duration is 2.029... hours.

diff.total_seconds() / (60 * 60)

Check if the diff is smaller or equal to the 5 minute duration.

if diff <= five_minute_duration:
    print("Yes")
else:
    print("No")
NHorner
  • 26
  • 2
  • 4
0

You can do compare operation before minus operation. Example is as shown below.

from datetime import datetime

dt1 = datetime(2020, 4, 19, 12, 20)
dt2 = datetime(2020, 4, 21, 15, 2)

def get_date_diff(date1, date2):
    diff = date2 - date1 if date1 < date2 else date1 - date2
    return diff

def test():
    print (get_date_diff(dt1, dt2))

test()
zuo
  • 46
  • 3
0

Create an upper and lower bound by creating two timedeltas with positive and negative values for the allowed difference

lower = timedelta(minutes=-5)
upper = timedelta(minutes=5)

Then check if the difference between the datetimes falls within those bounds

lower < date1 - date2 < upper

The advantage to this approach is that if this comparison is made many times lower and upper can be created just once and no sorting of the datetimes has to be performed

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50