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

Date time difference in minutes python

I’m new to python and tiring to calculate time difference in minutes. Times are in slightly two different formats. I have tried below to format but did not got it correctly

from datetime import datetime
start = "2023-05-21T11:40:54.532203+00:00"
end = "2023-05-21T11:45:00+00:00"

starttime=datetime.strptime(start , "%Y-%m-%dT%H:%M:%S:%f%z'")
endtime=datetime.strptime(end , "%Y-%m-%dT%H:%M:%S:%f%z'")

>Solution :

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

start = "2023-05-21T11:40:54.532203+00:00"
end = "2023-05-21T11:45:00+00:00"


def time_diff(start,end):
    start = datetime.strptime(start,"%Y-%m-%dT%H:%M:%S.%f%z")
    end = datetime.strptime(end,"%Y-%m-%dT%H:%M:%S%z")
    ##return in minutes
    return (end - start).total_seconds() / 60

print(time_diff(start,end))
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