Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subtract 2 datetime string
#1
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
Reply
#2
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
jss likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
That datetime string is so close to being iso format that it makes me wonder if the is a typo.
Reply
#4
(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
Reply
#5
FYI:

Here's a good read on datetime methods
datetime — Date and Time Value Manipulation
menator01 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting year from a string using strptime and datetime builtin Drone4four 3 8,736 Aug-11-2020, 12:02 PM
Last Post: ndc85430
  How to subtract the DayOfWeek from date from a integer? DarkCoder2020 2 2,134 May-27-2020, 09:56 PM
Last Post: bowlofred
  Subtract 11 from entire list of quoted numbers Pleiades 1 1,713 Nov-14-2019, 10:26 AM
Last Post: Larz60+
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,630 Sep-18-2019, 08:32 AM
Last Post: buran
  Subtract value from random dictionary key:value Tolein 1 2,493 Jun-12-2019, 02:02 PM
Last Post: perfringo
  Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo kneesarethebees 1 4,452 Aug-02-2018, 01:39 AM
Last Post: ichabod801
  Subtract Minutes from Datetime.Time tkj80 2 43,065 May-11-2017, 09:56 AM
Last Post: klllmmm
  Convert String to Datetime Object tkj80 2 33,830 Apr-20-2017, 08:45 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020