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

Groovy formula - date format

Shiva Jamei September 29, 2022

I am using this formula for a  calculated custom field (duration date calculation).

I need  the number of "days" that has passed since a date. In my case the custom field "Race Handover" is a date that we insert manually, and I need to see each day, how many days have passes since the handover date.

Here is what I put in the formula:

secondsBetween(issue.get("RACE Handover"), new Date(), "days" )

But what I am getting in Jira is the duration in Weeks format

so it shows: "26 weeks"

instead I would like to get: 182 days.

I wonder if you can help me understand how to change the format of the duration from weeks to days.

 

Thanks.

 

2 answers

1 accepted

1 vote
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 11, 2022

Hi @Shiva Jamei 

unfortunately, you cannot control the formatting of Calculated Duration fields. If you always want to display the duration in weeks and weeks only, whether the value is 1s (-> 0 weeks) or 10 years (-> 520 weeks), then you should create a calculated Number field and have it return the number of weeks between the two dates:

secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 / 24 / 7

and then use the formatting option to display:

$value weeks 
Shiva Jamei October 26, 2022

Thanks  @David Fischer I have created the calculated number field

This is the formula that I put:

secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 / 24 / 7 * 7 and here is the output:

In RACE since:       [The old field to calculate the duration]

23 weeks, 2 days

RACE duration days:  [The new field ]

161

So it is getting the 23 weeks and showing  23*7 as 161 days, but it is not adding the "2days", so I expect to see 163 days instead of 161.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 26, 2022

That's because, at the end, you divide by 7 and then multiply by 7. That rounds up the value to whole weeks. Just remove /7*7 at the end. 

Shiva Jamei October 27, 2022

This is what I get then by this:

 secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 /24

In RACE since: 19 weeks, 1 day

RACE duration days: No such property: weeks for class: script16668541552001129910092

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 27, 2022

Are you sure you didn’t remove the double quotes around weeks ?

Shiva Jamei October 27, 2022

groovy.jpg

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 27, 2022

It's the format expression that's incorrect, it should be a valid groovy expression. Try this:

"$value weeks"
Shiva Jamei October 27, 2022

Thanks, this is becoming interesting, I change the format, and now I get: 133.0  weeks

groovy 1.jpg

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 27, 2022

If you need weeks, the value should return a number of weeks, so you should add /7 to the formula. 

As for not displaying the ".0", in the format expression, try this:

"${(int)value} weeks"
Shiva Jamei October 27, 2022

Thanks @David Fischer 

It is fixed now, I did not wanted it in weeks as the original field is already in weeks and I wanted the new calculation in Days, so I just replaced weeks in days and now I get the correct number! groovy 2.jpg

which is 

groovy 3.jpg

 

Thanks a lot! 

0 votes
Paloma Fondon Araujo
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.
October 8, 2022

Hi @Shiva Jamei 

Here is an example of a script from the adaptavist library:

Or If you have automation for jira, you can create the following rule. You could include a trigger that runs every day at 9:00 am (for example) and enter a JQL that contains all issues that need to be updated. (or the trigger you already have included in the rule)

To calculate how many days have passed since the Handover Date you can include the following formula where customfield_XXXXX is your Handover Date field:

{{customfield_XXXXX.diff(now).days}}

Days from Date to now.png
I hope it helps,
Regards

Shiva Jamei October 11, 2022

Thanks Paloma,

 I have tried the adaptivist formula, but it didn't work, I don't know if it is compatible with "JMCF" scripts.

This is the custom field that I am actually using now and in the groovy script I put "secondsBetween(issue.get("RACE Handover"), new Date(), "days" )" 

jira.jpg

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events