GideDatetime giving wrong duration

Kishor O
Tera Sage

I am using below script to get time difference and getting duration like below image.

 

var gdt1 = new GlideDateTime('2024-05-28 04:20:01');
    var gdt2 = new GlideDateTime();
    var duration2 = GlideDateTime.subtract(gdt2, gdt1);
	gs.print(gdt2);
	gs.print(duration2);

 

KishorO_0-1716885374279.png

 

4 REPLIES 4

SN_Learn
Kilo Patron
Kilo Patron

Hi @Kishor O ,

 

Please try the below:

var gdt1 = new GlideDateTime('2024-05-28 04:20:01');
    var gdt2 = new GlideDateTime();
 
	var dur = GlideDateTime.subtract(gdt1 , gdt2); 
  gs.print("Diff: ="+dur.getDisplayValue());

 

Please mark this response as correct or helpful if it assisted you with your question.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

@SN_Learn How can I get the output in seconds or miliseconds?

Hi @Kishor O ,

 

Try this:

var gdt1 = new GlideDateTime('2024-05-28 04:20:01');
var gdt2 = new GlideDateTime();
 
var dur = GlideDateTime.subtract(gdt1 , gdt2); 
gs.print("Diff: ="+dur.getDisplayValue());

gs.print(gdt1.getNumericValue()); //In milliseconds
gs.print(gdt2.getNumericValue()); //In milliseconds

var sec = gdt1.getNumericValue();
gs.info(sec/1000);//In seconds

 

Please mark this response as correct or helpful if it assisted you with your question.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Hi @Kishor O ,

Thanks for marking it as helpful.

Could you please accept it as solution and close the thread? Thanks

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.