How to calculate ticket age with script runner

Ran Alkalay January 15, 2017

Hi,

We have purchase Script Runner Addon and we would like to have an Age column using Scripted field.

Currently we are using the script below but the returned format is long  (e.g. 25w 3d 14h 9m) and we would like to have it in days only.

Can you please assist how to tweak the script in order to gain this result ?

import com.atlassian.core.util.DateUtils
 
DateUtils.getDurationString(((new Date().getTime() - issue.getCreated().time) / 1000) as Long)

2 answers

1 accepted

3 votes
Answer accepted
JamieA
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 17, 2017
import org.joda.time.Days
import org.joda.time.LocalDate

int days = Days.daysBetween(LocalDate.fromDateFields(issue.getCreated()), LocalDate.fromDateFields(new Date())).getDays()

but calculated field are not good for temporal data - the value is only reindexed when the issue is updated, which means you can't query on them properly.

Ran Alkalay January 21, 2017

Indeed, part of the tickets are showing incorrect value which being changed any search on the filter.

Do you have other recommendation ?

JamieA
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 22, 2017

Depends what you want it for. If you want to query on it you can write a JQL function. If reporting maybe a dashboard gadget. Why is such a field useful?

Ran Alkalay January 22, 2017

For Dashboard usage, we would like to point days back

 

0 votes
Nic Brough -Adaptavist-
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 15, 2017

I think you could simply drop the whole getDurationString and just round it up

round ( ( new Date().getTime() - issue.getCreated().time) / 86400000 )

(86400000 = number of milliseconds in a day)

Ran Alkalay January 21, 2017

Thanks Nic, The problem that I had is after a week it started to show incorrect value of certain tickets.

Nic Brough -Adaptavist-
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 22, 2017

Incorrect how?  Random numbers, or not changing when you update the issues?

Ran Alkalay January 22, 2017

Random, the value was supposed to be 11 and it was changing all the time 4,6,5,7.

Once I took once of the tickets and used it on the preview on the scripted Field form it calculated the right value.

However, when I got back to JIRA filter it was again random.

I changed the script to the one  @Jamie Echlin [Adaptavist] provided and it fixed the value for all (I think it just refreshed all the values not that the logic on your script is incorrect)

Suggest an answer

Log in or Sign up to answer