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

Jakob KN
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 13, 2020

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 { it.name == firstDate }
def secondDateObject = customFieldManager.getCustomFieldObjects(issue).find { it.name == 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 5.5.7.1-jira8

2 answers

1 accepted

4 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 13, 2020

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()
Jakob KN
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 14, 2020

That did the trick, thanks Peter-Dave!

Iram Masood August 21, 2020

Can i  get the similar one but for JIRA cloud.

Jakob KN
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2020

Hi Iram, 

As far as i know, if you have the Scriptrunner app for Jira Cloud, you should be able to script a field like this.

You can see the documentation for the app here:
https://scriptrunner-docs.connect.adaptavist.com/jiracloud/scripted-fields.html

The Atlassian Marketplace listing for Cloud is found here:
https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira?hosting=cloud&tab=support

0 votes
Amit Shaw August 17, 2023

Hi Team, 

Can I get the script to add time field + number field. 

Basically I want to add start date of the task + duration of the task

Suggest an answer

Log in or Sign up to answer