Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the time difference between 2 sets of datetime objects in hours? Python

I’m connecting to an API that returns a list of articles, each article has created_at and modified_at properties. I want to return the difference (in hours) between the created_at and modified_at times for each article.

I attempted this but when checking, the time between values are incorrect. I think this is because I’m not looking at each article here, but maybe the categories.

First I got the date each article was created, these values are correct:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from datetime import datetime

rArticles = requests.get("https://....", auth = (apiKey, password))
responseArticles = rArticles.json()

        dateCreated = []
        for art in responseArticles["articles"]:
           dateCreated.append(art["created_at"])
           for ts in dateCreated:
             datetimeObjectCreated = datetime.strptime(ts,'%Y-%m-%dT%H:%M:%SZ')

This returns: enter image description here

Then I got the date each article was modified, these values also seem correct, might be worth noting if the article hasn’t been modified the date will just be the same as the created:

       dateModified = []
       for art2 in responseArticles["articles"]:
          dateModified.append(art2["modified_at"])
          for ts2 in dateModified:
             datetimeObjectModified = datetime.strptime(ts2,'%Y-%m-%dT%H:%M:%SZ')

This returns:enter image description here

Attempting to get the date between:

timeBetween = datetimeObjectModified - datetimeObjectCreated

This returns: enter image description here

which is not correct, as for example when looking at the 4th one down, the time between 2020-08-06 and 2020-08-06 is not 1213 days.

I also tried this way:

for time in datetimeObjectCreated:
   timeBetween = datetimeObjectModified - datetimeObjectCreated

but this returns an error: enter image description here

Thank you

>Solution :

Unless I’m missing something about your intentions, you’d just want

from datetime import datetime

for art in responseArticles["articles"]:
    ctime = datetime.strptime(art["created_at"], "%Y-%m-%dT%H:%M:%SZ")
    mtime = datetime.strptime(art["modified_at"], "%Y-%m-%dT%H:%M:%SZ")
    mod_hours = (mtime - ctime).total_seconds() / 3600
    print(f"{ctime=}, {mtime=}, {mod_hours=}")

You’ll need to modify that with a suitable if art.get("modified_at") if not all articles have a modification time, etc.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading