Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Misc Custom Fields calculated date (add days)

Shanelle Boluyt October 12, 2017

Hello,

 

I have installed the Misc Custom Fields add-on for Jira, but need some guidance on the correct formula to use to produce a date that is X days after another date field.

 

For example, if an email was sent on 1/1/17, the response would be due 14 days later, and I would want the calculated field to show 1/15/17.

 

Any suggestions?

 

Thanks,

 

Shanelle

4 answers

0 votes
David Hogan
Contributor
April 9, 2018

Hey all -

After a little struggle,  I think this does the trick really well. Change out your custom field number and issue types.

<!-- Calculate new Date based on Custom Field Date Time and Issue Type  - David Hogan 04/09/2018
@@Formula:
String issueType = issue.get("issuetype").getName().toString();
var d = new Date(issue.get("customfield_XXXXXX").getTime());

if (issue.get("customfield_XXXXXX") == null){
return null;
}
if (issueType.equals("YOUR ISSUE TYPE NAME")) {
// subtract 14 days from calculated date time custom field
d.setDate(d.getDate() - 14);
return d;
}
if (issueType.equals("YOUR ISSUE TYPE NAME")) {
// subtract 30 days from calculated date time custom field
d.setDate(d.getDate() - 30);
return d;
}
-->
Your Calculated Description Goes Here

David Hogan
Contributor
April 9, 2018

sidebar: thanks to those who contributed before me. Really appreciated. However, please note that the calculations of days*hours*seconds*milliseconds fail if greater than 24 days. Works great 1 through 24. If 25 days, it fails. 

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.
April 9, 2018 edited

That's correct. The formula should be altered slightly:

25*24L*3600*1000

to make sure calculations are done using Long instead of Integer. Otherwise, you reach the limit of Integers, which is 2^31-1 (a little over 2 billions).

David Hogan
Contributor
April 9, 2018

Thanks, David!

For others, here you go, this version works great too.

<!-- Calculate new Date based on Custom Field Date Time and Issue Type  - David Hogan 04/09/2018
@@Formula:
String issueType = issue.get("issuetype").getName().toString();

if (issue.get("customfield_XXXXXX") == null){
return null;
}
if (issueType.equals("YOUR ISSUE TYPE NAME")) {
// subtract 14 days from calculated date time custom field
return new Date(issue.get("customfield_XXXXXX").getTime() - 14*24L*60*60*1000);
}
if (issueType.equals("YOUR ISSUE TYPE NAME")) {
// subtract 30 days from calculated date time custom field
return new Date(issue.get("customfield_XXXXXX").getTime() - 30*24L*60*60*1000);}

-->
Your Calculated Description Goes Here

David Hogan
Contributor
April 9, 2018

David - thoughts on how to remove weekends and holidays efficiently?

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.
April 9, 2018

There is another Community question about weekends (with a solution) but not Holidays. It also depends on how you define Holidays, obviously, which is why it's not an easy problem.

0 votes
Shanelle Boluyt January 22, 2018

Thanks!

Shanelle Boluyt February 1, 2018

I have confirmed this works!

0 votes
Shanelle Boluyt January 18, 2018

Nothing with this add-on.  I did see that the (paid) Jira Workflow Toolbox add-on appears to have this functionality (on a transition), but I haven't had a chance to test.

 

https://bitbucket.org/fcarmario/jira-workflow-toolbox/wiki/others/Examples%20of%20Math-Time%20expressions

Kirti Vekriya January 19, 2018

There seems to be another which seemed to work for me. 

 

Get this add on: Jira Misc Workflow Extensions

And then on post functions you can do update field and select the custom date field you have created in this scenario "Lead time target date" and write the below

Lead time target date: {{ now | date('add', 14, 'days') | 'date') | date }}

 

This should populate the field but it is based on created date of the issue. 

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.
January 19, 2018 edited

Hi Kirti,

the syntax you provided is for Jira Cloud. On Jira Server, the syntax would be different:

new Date() + 14

As for JMCF, this is how you would do it. Imagine the field that holds the email date is called "Sent On":

if (issue.get("Sent On") == null)
return null;
return new Date( issue.get("Sent On").getTime() + 14*24L*3600*1000 );

Here's how it works: it returns a new Date object from milliseconds calculated from the milliseconds in the Sent On date + 14 days x 24 hours x 3600 seconds * 1000 milliseconds.

[EDIT] changed "24" into "24L" to force Long calculations, otherwise the formula won't work past 24 days (because the number of milliseconds would be more than the maximum Integer value, 2^31-1)

Kirti Vekriya January 19, 2018

Oh yes sorry I forgot to mention it is for the cloud version. Thank you for giving the server version info

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.
January 19, 2018

Well, I knew it had to be on Jira Server because JMCF is only available for Jira Server, not Cloud...

0 votes
Kirti Vekriya January 18, 2018

I need some assistance with this too, did you find a solution by any chance?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events