It's all about the answers!

Ask a question

Need help with adding days to custom date attribute in calculated value script


Michael Angelastro (55517) | asked Feb 22 '17, 11:58 a.m.

I've been tasked with writing a script that will pull a date from a custom attribute called "Finding Date" and adding 'X' number of days depending on the value of another field called 'Risk'. 

When debugging the script the Finding Date and Risk value is found correctly but it's having trouble adding x days to the finding date. 

 If the value from the risk drop list is "High" then 15 days need to be added to the finding date to get the 'Vulnerability Expiration Date"


here's the part of the script that's running into an exception,

//variable for number of days added to High Risk findings
var numberOfDaysToAdd = 15

I've tried both the following formats to get vExpirationDate:

var vExpirationDate = new Date(findingdate.setDate(findingdate.getDate() + numberOfDaysToAdd));
 
and

 var vExpirationDate = findingdate.setDate(findingdate.getDate() + numberOfDaysToAdd);

so if the finding date is 2/22/17 and the number of days to add is 15, the vExpirationDate should = 3/9/17

Both are getting exceptions.  Any help would be appreciated

Accepted answer


permanent link
Andy Chen (812) | answered Feb 23 '17, 1:06 a.m.

Hi,

Could you elaborate on what kind of exception you're getting?

A possible cause for the problem might be that the value you are pulling from the custom attribute "Finding Date" is not a proper Date object.

I've also checked the script and it should be working as expected if you're using the first format i.e.

var vExpirationDate = new Date(findingdate.setDate(findingdate.getDate() + numberOfDaysToAdd));

setDate() returns an integer denoting the milliseconds between 1 January 1970 00:00:00 UTC and the given date, so your second format would result in "vExpirationDate" being a large integer instead of a Date object.

Regards,
Andy

Michael Angelastro selected this answer as the correct answer

Comments
Michael Angelastro commented Mar 02 '17, 7:23 p.m.

 Thanks Andy.  The first format worked!  The reason it wasn't working originally was due to one of my variables.  "findingdate" should have been "findingDate"

One other answer



permanent link
Ralph Schoon (63.3k33646) | answered Feb 23 '17, 3:34 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Feb 23 '17, 3:34 a.m.

 I would suggest to use dojo/date and use functions such as add and difference and the like: https://dojotoolkit.org/reference-guide/1.10/dojo/date.html . I have used dojo/date to calculate the difference and it worked for me.


Comments
Michael Angelastro commented Mar 02 '17, 7:24 p.m.

 Thanks, Ralph!  I'll use this link for future date calculations

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.