Add Hours to a Date field on a WF Timer Script

jsummers
Kilo Contributor

Hello,

 

I'm using a Workflow Timer to wait for a date field but need to add hours to the Date, and can't seem to figure this one out. I'm referencing a variable (emp_start) that is a Date field on the RITM that the workflow is running on but it's not waiting for that date and proceeding through immediately. I'm wanting the workflow to continue at 9am on the emp_start date.

 

Here is what I've come up with so far, although fairly new to scripting, any help would be greatly appreciated:

 

var getDate = current.variables.emp_start;

var setDate = getDate.getGlideObject();

setDate.setHours(09);

setDate.setMinutes(00);

    var startDate = gs.dateDiff(gs.nowDateTime(), setDate, true);

        answer = startDate;

 

Thanks!

-Jon

1 ACCEPTED SOLUTION

Nope, the issue is the date format you're using, versus what my script is expecting.


Change this line:


var splitDate = getDate.split("-"); // Split Catalog Date into year, month, day  




To this line:


var splitDate = getDate.split("/"); // Split Catalog Date into year, month, day




Then change this line:


var startDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]);




To this line:


var startDate = new Date(splitDate[2],splitDate[0]-1,splitDate[1]);




Your date format on your instance is different than mine, hence the issue.


View solution in original post

16 REPLIES 16

Why I can't get the advanced editor everywhere, I'll never understand.



Here's the whole script edited for your instance:



// get date entered in catalog  


var getDate = current.variables.emp_start.getDisplayValue();  


var timeNow = new Date(); // Current Date/Time  


var splitDate = getDate.split("/"); // Split Catalog Date into year, month, day  


var startDate = new Date(splitDate[2],splitDate[0]-1,splitDate[1]); // Avoid timezone issues, but month starts at 0  


startDate.setHours(9);  


 


var dif = startDate.getTime() - timeNow; // Difference in milliseconds  


 


var Seconds_from_T1_to_T2 = dif / 1000; // Convert to seconds  


answer = Math.abs(Seconds_from_T1_to_T2); // Make sure it's not negative  


 


workflow.debug('Catalog date: ' + getDate);  


workflow.debug('Timer will end @: ' + startDate + '   ' +    


    (answer/60) + ' minutes from now' );  


Hi Rob,

I was trying to do something same for my off boarding process and your code worked perfectly.

Could you please help me understand how you are fixing the timezone to GMT -7 PDT in your script please, and if i want this to be based on selected user's timezone what changes are required.

Regards,

Sonali