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 DatesecondDateValue - 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
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.