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 add the seconds to the date and time ?

snow57
Tera Contributor

HI,

 

I am running the below code in the background script , but resulting as undefined. Could someone please help me to resolve this .

var d1 = '2023-09-22 21:13:58';
var d2 = '2023-09-22 21:43:58';
var d3 = '2023-09-23 21:43:58';
var dr ;
dr = gs.dateDiff(d1,d2,true);
d3.addSeconds(dr);
gs.print('dr' + d3.getValue());
 
Thanks,
 
5 REPLIES 5

Pavankumar_1
Mega Patron

Hi @snow57 ,

you can't get time difference for string fields and use below script.

var d1 = '2023-09-22 21:13:58';
var d2 = '2023-09-22 21:43:58';
var d3 = '2023-09-23 21:43:58';

var gdt1= new GlideDateTime(d1);//need to conveet into glidedatetime
var gdt2= new GlideDateTime(d2);
var gdt3= new GlideDateTime(d3);
var dur=new GlideDateTime.subtract(gdt1,gdt2);
var secs=dur.getNumericValue()/1000;//get the difference in seconds
gdt3.addSeconds(secs);
gs.info(gdt3.getValue());

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan,

 

Thanks for responding. I am able to get the difference in seconds. But i cant add that seconds to the other date. Could you please help me on this.

 

Thanks,

Hi @snow57 ,

I used the same script and able to add secs to date please refer below screenshot.

Screenshot (91).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan,

 

Thanks for helping me on this. Suppose after adding if i want to set in date and time variable by formatting . It is not resulting the expected result. 

 

Regards,

snow.