Current date/time to compare with end date/time minus 3 days

Vijay Baokar
Kilo Sage

Hi Folks,

 

I have below script in wait for condition activity of wf, where i need to check if currentDateTime == endDateTime - 3 days

 

Suppose "current.variables.end" is 2024-08-30 10:30:00

current date/time is 2024-08-26 10:30:00

 

result: This script should trigger next activity when current datetime reaches 2024-08-27 10:30:00 , that is 3 days minus 2024-08-30 10:30:00, BUT ITS NOT WORKING, Please suggest the correction.

 

answer = ifScript();
function ifScript() {
    var currentDateTime = new GlideDateTime(); // Gets the current date and time
    var endDateTime = new GlideDateTime(current.variables.end); // Gets the end date and time from the variable
    //var diffdate = endDateTime - 3
    endDateTime.addDaysUTC(-3); //subtract 3 days with time from endDateTime

    var timediff = endDateTime;
    gs.log('TIME Diff IS :',timediff);
   
    gs.log('-3days',endDateTime);
    //if ( currentDateTime == timediff)
    if ( currentDateTime == timediff)
    {
        return 'yes';
    }

   
}
7 REPLIES 7

 

VijayBaokar_1-1724658915716.png

Hi @Ravi Gaurav  Please check this.

That's strange.. 
I did more changes and hit and trail.. use the below 

 

function ifScript() {
var currentDateTime = new GlideDateTime();
var endDateTime = new GlideDateTime(current.variables.end);
endDateTime.addDaysUTC(-3);

gs.log('Current DateTime: ' + currentDateTime.getDisplayValue());
gs.log('End DateTime minus 3 days: ' + endDateTime.getDisplayValue());

var tolerance = 60 * 1000;
var timeDiff = Math.abs(currentDateTime.getNumericValue() - endDateTime.getNumericValue());

if (timeDiff <= tolerance) {
return true;
}

return false;
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/