# How do I find the time difference between two datetime objects in python?

How do I tell the time difference in minutes between two `datetime` objects?

17
``````>>> import datetime
>>> first_time = datetime.datetime.now()
>>> later_time = datetime.datetime.now()
>>> difference = later_time - first_time
datetime.timedelta(0, 8, 562000)
>>> seconds_in_day = 24 * 60 * 60
>>> divmod(difference.days * seconds_in_day + difference.seconds, 60)
(0, 8)      # 0 minutes, 8 seconds
``````

Subtracting the later time from the first time `difference = later_time - first_time` creates a datetime object that only holds the difference. In the example above it is 0 minutes, 8 seconds and 562000 microseconds.

Tuesday, June 1, 2021

93

You can convert them to timestamps and go from there:

``````\$hourdiff = round((strtotime(\$time1) - strtotime(\$time2))/3600, 1);
``````

Dividing by 3600 because there are 3600 seconds in one hour and using `round()` to avoid having a lot of decimal places.

Thursday, June 3, 2021

79

The following example demonstrates how to do this:

``````DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
Console.WriteLine(b.Subtract(a).TotalMinutes);
``````

When executed this prints "30" since there is a 30 minute difference between the date/times.

The result of `DateTime.Subtract(DateTime x)` is a TimeSpan Object which gives other useful properties.

Thursday, June 3, 2021

91

I like your idea of using a `try..except` to validate the input, since in some future version of Python, years < 1000 might be acceptable.

This comment in the code suggests this restriction is limited to Python's current implementation of strftime.

In Python 2.7, the exception occurs for `years < 1900`, but in Python 3.2, the exception occurs for `years < 1000`:

``````import datetime as dt
format = "%Y-%m-%d"
t = dt.datetime.strptime("0023-10-10", format)
try:
t.strftime(format)
except ValueError as err:
print(err)
``````

prints

``````year=23 is before 1000; the datetime strftime() methods require year >= 1000
``````
Saturday, November 6, 2021

80

You probably have a different locale set up. %B is March in locales that use English, but in other locales it will fail.

For example:

``````>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>>
>>> data = ['January 20 1999', 'March 4 2010', 'June 11 1819']
>>> for item in data:
...     print datetime.datetime.strptime(item, '%B %d %Y')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.6/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'January 20 1999' does not match format '%B %d %Y'
``````

Here you see that even though the format does match, it claims it doesn't. And that's because the month names doesn't match. Change it to Swedish locale names, and it works again:

``````>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>>
>>> data = ['Januari 20 1999', 'Mars 4 2010', 'Juni 11 1819']
>>> for item in data:
...     print datetime.datetime.strptime(item, '%B %d %Y')
...
1999-01-20 00:00:00
2010-03-04 00:00:00
1819-06-11 00:00:00
``````

(Note that the above locale 'sv_SE.utf8' might not work for you, because you have to have that specific locale installed. To see which ones that are installed on a Unix machine, run this command from the command line:

``````\$ locale -a
C
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX
sv_FI.utf8
sv_SE.utf8
``````

)

Thursday, November 18, 2021