Forums

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

Subtracting more than 24 days from a custom field using

David Hogan
Contributor
April 6, 2018

I have a script that works really well to subtract days from a custom field. That is, until I need to subtract more than 24 days.

Here are the details:

customfield_14082 = 2018-09-15 00:00

Subtracting 14 days script returns 2018-09-01 00:00 - PERFECT! Works 1 thru 24 days.

Subtracting  25 (or greater same issue) returns  2018-10-9 17:02 - What? Why?

 

Here is the script:

<!-- Subtracts two weeks from the New Business Date (customfield_14082) - David Hogan 04/06/2018
@@Formula:
if (issue.get("customfield_14082") == null)
return null;
long ms = issue.get("customfield_14082").getTime() - 25*24*60*60*1000;
date = new Date(ms);
return date;
-->

 

Any assistance appreciated!

David

 

1 answer

1 accepted

1 vote
Answer accepted
Tarun Sapra
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 7, 2018

I presume the script is using the groovy interpreter, and if that's the case then it's very easy to subtract the days and the get the correct answer.

Sample - to subtract 25 days

use (groovy.time.TimeCategory) {
  def date = new Date()
  println date
  println date-25.days
}

https://stackoverflow.com/questions/8816990/subtract-a-date-by-1-or-2-in-groovy

 

And it's super easy in groovy to get date object from the String using Date.parse method

Sample here - http://rmr.wikia.com/wiki/Groovy_Date_Parsing_and_Formatting

Thus, once you have the date object (after converting string format of customField.value()) then you can just subtract the 25 days and that should get you the correct value as that is more idiomatic way of doing things.

David Hogan
Contributor
April 9, 2018

Thanks for the reply, Tarun!

After some troubleshooting, I came up with this.

<!-- 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;
}
-->

Like Tester Testson likes this
Tarun Sapra
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 10, 2018

Hi @David Hogan

Is this working now for you?

David Hogan
Contributor
April 10, 2018

Hi Tarun - Yes, the above script works. Thanks!

Tarun Sapra
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 10, 2018

Glad that it's working, could you please upvote/accept the answer so that other users with similar issue at hand are helped.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events