Python Forum
subtract 2 datetime string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: subtract 2 datetime string (/thread-40950.html)



subtract 2 datetime string - jss - Oct-18-2023

I have a code like below
from datetime import datetime
time_uploaded="2023-09-26T11:22:42.2Z"
time_now =datetime.now()
i now want to subtract the above 2 datetime ie (time_now-time_uploaded) and the output should be in days,hours,seconds,minutes format
How do i do that in python


RE: subtract 2 datetime string - menator01 - Oct-18-2023

One way
from datetime import datetime

current = datetime.now()

upload = datetime.strptime('2023-09-26T11:22:42.2Z', '%Y-%m-%dT%H:%M:%S.%fZ')

print(f'Uploaded: {upload}')
print(f'Current: {current}')

print(f'Difference: {current - upload}')
Output
Output:
Uploaded: 2023-09-26 11:22:42.200000 Current: 2023-10-18 10:50:13.519030 Difference: 21 days, 23:27:31.319030

Edited a little from an example I found
from datetime import datetime


upload = datetime.strptime('2023-09-26T11:22:42.2Z', '%Y-%m-%dT%H:%M:%S.%fZ')
current = datetime.now()
diff = current - upload

def fmt(tdelta, fmt):
    times = {'days':tdelta.days}
    times['hours'], rem = divmod(tdelta.seconds, 3600)
    times['minutes'], times['seconds'] = divmod(rem, 60)
    return fmt.format(**times)

print(f'Uploaded: {upload}')
print(f'Current: {current}')
print(fmt(diff, 'Difference {days} days {hours} hours {minutes} minutes {seconds} seconds'))
Output
Output:
Uploaded: 2023-09-26 11:22:42.200000 Current: 2023-10-18 14:31:03.077798 Difference 22 days 3 hours 8 minutes 20 seconds



RE: subtract 2 datetime string - deanhystad - Oct-18-2023

That datetime string is so close to being iso format that it makes me wonder if the is a typo.


RE: subtract 2 datetime string - jss - Oct-19-2023

(Oct-18-2023, 03:50 PM)menator01 Wrote: One way
from datetime import datetime

current = datetime.now()

upload = datetime.strptime('2023-09-26T11:22:42.2Z', '%Y-%m-%dT%H:%M:%S.%fZ')

print(f'Uploaded: {upload}')
print(f'Current: {current}')

print(f'Difference: {current - upload}')
Output
Output:
Uploaded: 2023-09-26 11:22:42.200000 Current: 2023-10-18 10:50:13.519030 Difference: 21 days, 23:27:31.319030

Edited a little from an example I found
from datetime import datetime


upload = datetime.strptime('2023-09-26T11:22:42.2Z', '%Y-%m-%dT%H:%M:%S.%fZ')
current = datetime.now()
diff = current - upload

def fmt(tdelta, fmt):
    times = {'days':tdelta.days}
    times['hours'], rem = divmod(tdelta.seconds, 3600)
    times['minutes'], times['seconds'] = divmod(rem, 60)
    return fmt.format(**times)

print(f'Uploaded: {upload}')
print(f'Current: {current}')
print(fmt(diff, 'Difference {days} days {hours} hours {minutes} minutes {seconds} seconds'))
Output
Output:
Uploaded: 2023-09-26 11:22:42.200000 Current: 2023-10-18 14:31:03.077798 Difference 22 days 3 hours 8 minutes 20 seconds

Thank you .Your solution is working as expected


RE: subtract 2 datetime string - Larz60+ - Oct-19-2023

FYI:

Here's a good read on datetime methods
datetime — Date and Time Value Manipulation