Using Variable to Add Days with Script

melinda_owen
Giga Contributor

Hi, I'm not sure I'm posting this in the correct forum since I felt it could go in a number of places. My scenario is:

I have a Date variable (u_term_date) that gets filled in on the first task of my workflow, after my RITM has been created, it's for a contract termination date. I need my second task to wait to generate until 90 days after this term date variable. 

I have added a duration field to be hidden on my RITM so it can hold the value of the date for the 90 days added and the workflow Timer can wait until that duration field. I am having trouble with my script and would love some assistance and feedback, please.

Here is my script currently inside my Timer activity.

if(current.variables.u_term_date.getDisplayValue() < gs.nowDate()) {
 answer = true;
}
else{
answer = false;
}

I had included in my script the addDays() which it doesn't seem to be adding, since I included a gsLog after it to see if the addition happened, and it did not. It was still logging the original u_term_date. I am new to scripting inside ServiceNow, and more importantly, a complete novice to Date scripting. I have scoured the Community for related topics, and most are incorporating adding days/time to current date, which is not my goal. I've tried piecing together scripts and I've tried a couple different approaches, all to no avail. Any and all help is appreciated at this point.

If I need to include any further information, please let me know. Thank you

1 ACCEPTED SOLUTION

you need below script 

var now= new GlideDateTime();
var gdt = new GlideDateTime(current.variables.u_term_date); 
gdt.addDaysLocalTime(90); //adds 90 days to term date variable
var dur =  GlideDateTime.subtract(now, gdt);
answer = dur.getNumericValue();

View solution in original post

13 REPLIES 13

Thank you, I figured I had needed getDisplayValue(); and that is the first time I've seen getNumericValue(). I'm testing that now!

for timer activity it needs in seconds and getNumericValue() will give you that.

Hi, 

I am trying to write a script to add Days Variable value to  Date Variable 

 

i.e.   Add Days to { value } to  Date { 30.07.2021}

 

Hello Mike,

Thanks for the info.

I have same kind of requirement where adding 1 day timer to the given date.

I gave variable type as Date/Time on the form side. If I gave 03-25-2024 01:30 PM, the workflow should run at 03-26-2024 01:30 PM.

as of now I am using below script on timer activity on workflow side. The below script is resuming the timer activity at 12:00 AM 03-26-2024 which I don't want at 12:00 AM. it should run at a selected time.

var gdt = new GlideDateTime(current.date_variable);

gdt.addDaysLocalTime(1);

answer= gs.dateDiff(gs.nowDateTime(), gdt + "01:00:00", true);// here I don't want at 12:00 AM.I just want random time or at least time picked on the form should work. Ex: If I gave 03-25-2024 01:30 PM, the workflow should run at 03-26-2024 01:30 PM.

Can you please help with the  code.