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.

How to calculate time duration in seconds from now to a given DateTime

Jeny
Mega Contributor

Hi, I have a date/time variable as an input and I am trying to calculate the duration in seconds from the current moment (now) till the date/time selected in the date/time field.

I saw some examples and am currently trying with the below one but am getting the following error and cannot figure what is going wrong: 

org.mozilla.javascript.WrappedException: Wrapped org.mozilla.javascript.EvaluatorException: Can't find method com.glide.glideobject.GlideDateTime.subtract(java.lang.String,com.glide.glideobject.GlideDateTime).

Thanks!

Jeny

// Set 'answer' to the number of seconds this timer should wait

var gdt1 = GlideDateTime();
gs.addInfoMessage("date now: "+gdt1);

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.addInfoMessage("date now using internal value: "+gdt2);

var gdt = new GlideDateTime(current.variables.destroy_after.getValue());
var gdt3 = gdt.getDisplayValueInternal();
gs.addInfoMessage("workflow end date is: "+gdt3);

var duration = GlideDateTime.subtract(gdt3,gdt2);
gs.addInfoMessage("duration is: "+duration);

answer = (duration.getNumericValue() / 1000);
gs.addInfoMessage("answer is " + answer);

 

1 ACCEPTED SOLUTION

Jeny
Mega Contributor

Hi Alexey,

Your suggestion didn't work but this below worked eventually.

var gdt1 = GlideDateTime();
gs.addInfoMessage("date now: "+gdt1);

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.addInfoMessage("date now using internal value: "+gdt2);

var gdt3 = new GlideDateTime(current.variables.destroy_after).getDisplayValueInternal();
gs.addInfoMessage("workflow end date is: "+gdt3);

var dur_seconds = gs.dateDiff(gdt2, gdt3, true);
gs.addInfoMessage("duration is: "+dur_seconds);

answer = dur_seconds;
gs.addInfoMessage("answer is " + dur_seconds);



View solution in original post

4 REPLIES 4

Harshinya1
Mega Guru

This link has two approaches to calculate the difference

https://community.servicenow.com/community?id=community_question&sys_id=c5f54361db1cdbc01dcaf3231f96...

 

Hope this helps

Jeny
Mega Contributor

Hi Harshinya, 

Thanks for the link! I did some changes and this below worked for me, only I do not know how to take the gdt3 in my selected time zone and preserve that the var is an object. I get 2h difference currently.

var gdt1 = GlideDateTime();
gs.addInfoMessage("date now: "+gdt1);

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.addInfoMessage("date now using internal value: "+gdt2);

var gdt3 = new GlideDateTime(current.variables.destroy_after);
gs.addInfoMessage("workflow end date is: "+gdt3);

var dur_seconds = gs.dateDiff(gdt2, gdt3, true);
gs.addInfoMessage("duration is: "+dur_seconds);

answer = dur_seconds;
gs.addInfoMessage("answer is " + dur_seconds);

 

Hi,

you can use getTZOffset() method.

 

Jeny
Mega Contributor

Hi Alexey,

Your suggestion didn't work but this below worked eventually.

var gdt1 = GlideDateTime();
gs.addInfoMessage("date now: "+gdt1);

var gdt2 = new GlideDateTime(gdt1.getDisplayValueInternal());
gs.addInfoMessage("date now using internal value: "+gdt2);

var gdt3 = new GlideDateTime(current.variables.destroy_after).getDisplayValueInternal();
gs.addInfoMessage("workflow end date is: "+gdt3);

var dur_seconds = gs.dateDiff(gdt2, gdt3, true);
gs.addInfoMessage("duration is: "+dur_seconds);

answer = dur_seconds;
gs.addInfoMessage("answer is " + dur_seconds);