Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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 { 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

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!

Can i  get the similar one but for JIRA cloud.

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

Suggest an answer

Log in or Sign up to answer
TAGS

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