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

Scripted duration field, display time between two Date Time custom fields

Hi Community! 

I'm trying to create a scripted field with Scriptrunner, which shows the time difference between two Date Time custom fields (called Start Date & End Date).

I'm using the Duration template, since I want the field to be shown in the dates-panel on the right.

When i test my script against different issues, it always returns the correct number of days between the two dates, but followed by "Seconds".

The goal is to have the field shown "XX months XX weeks XX hours and XX minutes". 

My script looks as follows:

import com.atlassian.jira.component.ComponentAccessor

final String firstDate = "Start Date"
final String secondDate = "End Date"

def customFieldManager = ComponentAccessor.customFieldManager

def firstDateObject = customFieldManager.getCustomFieldObjects(issue).find { == firstDate }
def secondDateObject = customFieldManager.getCustomFieldObjects(issue).find { == secondDate }
if (!firstDateObject || !secondDateObject) {
return null

def firstDateValue = issue.getCustomFieldValue(firstDateObject) as Date
def secondDateValue = issue.getCustomFieldValue(secondDateObject) as Date

secondDateValue - firstDateValue

Any help or suggestion will be much appreciated.
I'm on Jira Data Center 8.2.3, with Scriptrunner

1 answer

1 accepted

1 vote
Answer accepted

Duration expects a <Long> number of seconds.
Jira stores/returns dates as java.sql.Timestamp
If you use getTime() you will get millisecond values since January 1, 1970, 00:00:00 GMT.

So putting that together, you can do this:

def durationMillis = secondDateValue.getTime() - firstDateValue.getTime()
def duration = (durationMillis/1000).toLong()

Or if you prefer a single line:

((secondDateValue.getTime() - firstDateValue.getTime())/1000).toLong()

That did the trick, thanks Peter-Dave!

Suggest an answer

Log in or Sign up to answer

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