Need help with adding days to custom date attribute in calculated value script
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
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
One other answer
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.