Calculating Time Difference in Jira Using Python

I’m looking for a way to determine the duration between when an issue is opened and when it gets resolved. I have the following data:

issue_start = issue.fields.created
issue_end = issue.fields.resolutiondate

When I print the values, I get:

Start: 2016-06-09T14:37:05.000+0200 End: 2016-06-10T10:53:12.000+0200

How can I subtract the time of resolution from the time of creation to calculate the total time spent on the issue?

To calculate the time difference, you can convert the date strings to Python datetime objects using the datetime module, and then subtract them to get a timedelta object. Here’s a quick way to do it:

from datetime import datetime

fmt = '%Y-%m-%dT%H:%M:%S.%f%z'

start_dt = datetime.strptime(issue_start, fmt)
end_dt = datetime.strptime(issue_end, fmt)

duration = end_dt - start_dt
print(f'Total time spent: {duration}')

This code will give you the duration in days, hours, minutes, etc. You can further extract the exact units of time as needed from the timedelta object.

Try checking if your Jira fields might be inconsistent, sometimes there are timezone offsets that can throw things off. double check to make sure both times are in the same time zone. That way, when you do subtraction with datetime, it gives the accurate duration! hope this helps!

To add on to the helpful insights, once you have obtained the timedelta object from subtracting the two datetime objects, if you wish to convert the duration into specific units like hours or minutes, you can do this by accessing properties of the timedelta:

# for hours
duration_hours = duration.total_seconds() / 3600
print(f'Total hours spent: {duration_hours}')

# for minutes
duration_minutes = duration.total_seconds() / 60
print(f'Total minutes spent: {duration_minutes}')

This approach can help you to easily integrate the calculated time back into your workflow or reporting systems.