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

Mark Manders
Mega Patron

It's a bit weird that your variables are set like this: endDateTime is now and currentDateTime is the given variable date/time.

Because of that, you are now checking if 'now' minus the variable time is something. You aren't sharing what you need, but at this moment, if the endDate is in the future, it will be negative, so the log will say 'no'. Only if it's 3 days in the past, you will get 'yes'.

I think it needs to be the other way around, but that's a guess. I would switch the variables you are setting so they make more sense.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ohh yes, it has to be like:

{
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');
}
}

Simon Christens
Kilo Sage

Hi Vijay

You need to be careful about setting a date time variable into GDT because if im not mistaken then it uses the displayValue and not actual UTC server formated value - try changing your date time format to validate.

Also - what do you expect as user inputs ? Always a date in the future or a date in the past ? - because it can have an impact in the way the difference is calculated
Have a look at something like this - maybe it helps:

var userInput = '23/08/2024 15:00:00';

var inputDate = GlideDateTime();
inputDate.setDisplayValue(userInput, gs.getDateTimeFormat()); //In case the variable is parsed as a display value then we need to ensure that GDT knows the format - else parse it as you did yourself

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

gs.print("diff " + duration1);
gs.info(parseInt(duration1.split(' ')[0]) > 3);

Hi @Simon Christens  i have made the logic where user can only select date in future. Like end date will always be after start date and then i am checking end date/time with current date/time.

note: start date also can't be in past.