Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date time declaration

Vijay Baokar
Kilo Sage

Hi Team,

 

I need some suggestion on below script in IF condition of workflow

 

answer = ifScript();

function ifScript()
{
var currentDateTime = new GlideDateTime('current.variables.endDate'); this should be the date/time user selects
var endDateTime = new GlideDateTime(); this should be the current date/time

var difference = endDateTime.getNumericValue() - currentDateTime.getNumericValue();

var differenceInDays = difference / (1000 * 60 * 60 * 24);

if (differenceInDays >= 3) {
    gs.info('Yes');
} else {
    gs.info('No');
}
}
 
is above declaration correct for date/time comparison in workflow ?
9 REPLIES 9

If its a future date then the below should always have a positive output

var duration1 = gs.dateDiff(now.getDisplayValue(), inputDate.getDisplayValue(), false);

 ex:

*** Script: diff 4 03:30:20

 And then by splitting it you can see if its more than 3 days - in the above example its 4

gs.info(parseInt(duration1.split(' ')[0]) > 3);

 

with my below script , its always returning NO even though end date/time and current date time has more than 3 days of diff. exp end date/time is "2024-08-23 15:14:06" and current date time is 2024-08-19 15:14:12.

if the diff is more than 3 days then it should return YES

 

answer = ifScript();

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

var difference = endDateTime.getNumericValue() - currentDateTime.getNumericValue();

var differenceInDays = difference / (1000 * 60 * 60 * 24);

if (differenceInDays >= 3) {
    gs.info('Yes');
} else {
    gs.info('No');
}
}

var endDateTime = new GlideDateTime('current.variables.end');

Remove the ' before and after current.variables.end

Hi @Simon Christens i didn't get your last comment. can you suggest what changes needs to be made on below syntax to achieve this?

 

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

var difference = endDateTime.getNumericValue() - currentDateTime.getNumericValue();

var differenceInDays = difference / (1000 * 60 * 60 * 24);

if (differenceInDays >= 3) {
    gs.info('Yes');
} else {
    gs.info('No');
}
}

You need to remove the ' before and after current.variables.end inside you endDateTime gdt

var endDateTime = new GlideDateTime('current.variables.end');