It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Calculate duration and write to a text field

I have two custom date fields (start and end).  I need to first calculate the duration (end-start) and then format that value like this:  d hh:mm:ss

Even if the duration is long, it would still be the above format so no months or years, just days hh:mm:ss.  If the duration was shorter than a day, the format would be hh:mm:ss.

I need to write the value to a text field.  So, the resulting text field would look something like this:  2  12:23:54  (2 days, 12 hours, etc)

I need to include this code in Post Function --> Scriptrunner.  Does anyone have a script handy that would help with this task?

1 answer

0 votes

You could use groovy.time.TimeCategory.minus(end, start) to get a groovy.time.TimeDuration

def endDate = new Date()
def startDate = new Date().parse("dd/MM/yyyy","11/1/2019")
groovy.time.TimeCategory.minus(endDate , startDate)

That script in the console outputs:  301 days, 9 hours, 44 minutes, 23.499 seconds

But you can combine different parts yourself to control the format:

def endDate = new Date()
def startDate = new Date().parse("dd/MM/yyyy","1/1/2018")
def duration = groovy.time.TimeCategory.minus(endDate , startDate)
"$duration.days $duration.hours:$duration.minutes:$duration.seconds"

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

308 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you