Help with GlideDateTime.subtract - not sure why this is failing

Carl Fransen1
Tera Guru

Hi Team,

Interesting one for y'all today.  I'm trying to subtract NOW (GlideDateTime) from a date field provided on my HR case - this is inside the workflow to calculate a timer value is seconds.

The script works when I use the below script:

var gdt1 = GlideDateTime();
gs.info("gdt date now " + gdt1); // logging info - can be removed later

var gdt3 = new GlideDateTime("2018-08-01 00:00:00");
gs.info("course end date is " + gdt3);  // logging info - can be removed later

var duration = GlideDateTime.subtract(gdt1, gdt3);
gs.info("duration is " + duration);  // logging info - can be removed later

answer = (duration.getNumericValue() / 1000);
gs.info("answer is " + answer);  // logging info - can be removed later

This returns the correct values into my log, so all good.  However the 'GlideDateTime' grabs the UTC time, not my system time, and we are in NZ so are 12 hours ahead.  So to counter this I run the code below to grab the value in my timezone and then try to subtract the values, see code below:

var gdt1 = GlideDateTime();
gs.info("gdt date now " + gdt1); // logging info - can be removed later

var gdt2 = gdt1.getDisplayValueInternal();
gs.info("gdt date now using internal value " + gdt2); // logging info - can be removed later

var gdt3 = new GlideDateTime("2018-08-01 00:00:00");
gs.info("course end date is " + gdt3);  // logging info - can be removed later

var duration = GlideDateTime.subtract(gdt2, gdt3);
gs.info("duration is " + duration);  // logging info - can be removed later

answer = (duration.getNumericValue() / 1000);
gs.info("answer is " + answer);  // logging info - can be removed later

All my log statements return a valid value, as per last time.  However now the 'subtract' doesn't work at all - as I'm trying to subtract 'gdt2' (which is now the 'getDisplayValueInternal' version of 'gdt1') from 'gdt3'.

And now I receive the following error:

Javascript compiler exception: Can't find method com.glide.glideobject.GlideDateTime.subtract(java.lang.String,com.glide.glideobject.GlideDateTime). (null.null.script; line 10)

Not sure if this is because although 'gdt2' looks the same it might be in a different format, but whatever it is I'm flummoxed.

Hoping someone can help with this...

Thanks
Carl.

 

1 ACCEPTED SOLUTION

Can we try like below, let's see if that helps.

var gdt1 = GlideDateTime();
gs.info("gdt date now " + gdt1); // logging info - can be removed later

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.info("gdt date now using internal value " + gdt2); // logging info - can be removed later

var gdt3 = new GlideDateTime("2018-08-01 00:00:00");
gs.info("course end date is " + gdt3); // logging info - can be removed later

var duration = GlideDateTime.subtract(gdt2, gdt3);
gs.info("duration is " + duration); // logging info - can be removed later

answer = (duration.getNumericValue() / 1000);
gs.info("answer is " + answer); // logging info - can be removed later

View solution in original post

4 REPLIES 4

Mike Patel
Tera Sage

Are you trying to subtract days or hours or seconds.

Hi Mike,

The field I'm grabbing the value from is 'Course End Date' and is a date field only.  I'm comparing this with the 'GlideDateTime' value I get, which is date/time, and then converting to a duration, then to seconds for my workflow timer.

My output shows the following:

*** Script: gdt date now 2018-07-31 00:58:00
*** Script: gdt date now using internal value 2018-07-31 12:58:00
*** Script: course end date is 2018-08-01 00:00:00
*** Script: duration is 1970-01-01 23:01:59
*** Script: answer is 82919.659

So you can see gdt1 and gdt2 (furst and second lines) show the values correctly, in standard and then timezone
converted time. It all works fine, except when I replace the first value shown with the second value - then
for some reason I receive the error shown in my original post.

In my example I'm just adding a value onto my script for variable 'gdt3', but in my workflow I grab the variable value.

Does this help?

Can we try like below, let's see if that helps.

var gdt1 = GlideDateTime();
gs.info("gdt date now " + gdt1); // logging info - can be removed later

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.info("gdt date now using internal value " + gdt2); // logging info - can be removed later

var gdt3 = new GlideDateTime("2018-08-01 00:00:00");
gs.info("course end date is " + gdt3); // logging info - can be removed later

var duration = GlideDateTime.subtract(gdt2, gdt3);
gs.info("duration is " + duration); // logging info - can be removed later

answer = (duration.getNumericValue() / 1000);
gs.info("answer is " + answer); // logging info - can be removed later

Thanks for your help Shishir - this has done the trick nicely!