Subtracting dates to get hours and minutes

Weatherby,Gerard gweatherby at uchc.edu
Mon Dec 12 12:49:32 EST 2022


The difference between two datetime objects is a timedelta object. https://docs.python.org/3/library/datetime.html#timedelta-objects . It has a total_seconds() method.

This is a simple task, unless one of the datetimes has a time zone specified and the other doesn’t.

From: Python-list <python-list-bounces+gweatherby=uchc.edu at python.org> on behalf of Marc Lucke <marc at marcsnet.com>
Date: Monday, December 12, 2022 at 11:37 AM
To: python-list at python.org <python-list at python.org>
Subject: Re: Subtracting dates to get hours and minutes
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

my approach would be to convert your two date/times to seconds from
epoch - e.g.
https://urldefense.com/v3/__https://www.geeksforgeeks.org/convert-python-datetime-to-epoch/__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNd7mAAYr$<https://urldefense.com/v3/__https:/www.geeksforgeeks.org/convert-python-datetime-to-epoch/__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNd7mAAYr$>  - then
subtract the number, divide the resultant by 3600 (hours) & get the
modulus for minutes.  There's probably a standard function - it should
be /very/ easy to do.

- Marc

On 12/12/2022 5:01 pm, Steve GS wrote:
> How do I subtract two time/dates and calculate the hours and minutes
> between?
> Steve
>
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNSbh5q0S$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNSbh5q0S$>


More information about the Python-list mailing list