Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

How to display script field in format "xx D YY h ZZ m QQ s"

Dear Community,

I have a field that calculates the difference between two dates:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.*
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateResponse = customFieldManager.getCustomFieldObjectByName('Date of First Response');

if(issue.getCustomFieldValue(dateResponse) && issue.created)
{
def dateValue= issue.getCustomFieldValue(dateResponse) as Date
def dateValue2= issue.created
def dif = dateValue.getTime() - dateValue2.getTime()
return dif
}

I want to display value of this field in format: "XX Days YY Hours ZZ Minutes QQ Seconds"

Anyone can help me out?

Thanks in advance.

2 answers

0 votes

You could use brute force to calculate the d/h/m/s from the raw number of seconds, but I'd use Jira's date formatter myself - https://docs.atlassian.com/software/jira/docs/api/7.12.1/com/atlassian/jira/util/JiraDurationUtils.html

@Nic Brough _Adaptavist_ I will be grateful for more directions. :)

There's sample code on https://scriptrunner.adaptavist.com/4.1.3.7/jira/working-with-tempo.html  (I know, a little odd to point you at Tempo integration, but actually the code is for any duration, not just Tempo data)

Thank you. It's working, but the calculation is wrong:

1st.jpg2nd.jpg

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.*
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateResponse = customFieldManager.getCustomFieldObjectByName('Date of First Response');

if(issue.getCustomFieldValue(dateResponse) && issue.created)
{
def dateValue= issue.getCustomFieldValue(dateResponse) as Date
def dateValue2= issue.created
def dif = dateValue.getTime() - dateValue2.getTime()
return dif as Long
}
#if($value)
$jiraDurationUtils.getFormattedDuration($value)
#end

 Any ideas why?

Now it is fixed.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.*
import java.util.Date.*
import com.atlassian.core.util.DateUtils

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateResponse = customFieldManager.getCustomFieldObjectByName('Date of First Response');

if(issue.getCustomFieldValue(dateResponse) && issue.created)
{
def dateValue= issue.getCustomFieldValue(dateResponse) as Date
def dateValue2= issue.created
def dif = dateValue.getTime() - dateValue2.getTime() as float
return DateUtils.getDurationString(Math.round(dif / 1000))
}

Script Field:

Template: Text Field (multi-line)

Searcher: Free text searcher

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

32 views 0 2
Read article

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