Difference between two dates

Mark Wood
Tera Contributor

Hello Experts,

I have written below to calculate the difference between the two dates can anyone why my script is not working?

Thank You.

Display Business Rule

var gd1=new GlideDateTime();
gd1.setDisplayValue(current.sys_created_on);
var gd2=new GlideDateTime();
gd2.setDisplayValue(current.sys_created_on);
var duration=GlideDateTime.subtract(gd1.gd2);
g_scratchpad.diff=duration.getDisplayValue();

Onload Client Script:

function onLoad()
{
var d=g_scratchpad.diff;
alert(d);


}

 

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage
Giga Sage

Hi @Mark Wood ,

 

There are few errors please correct it,

 

1. you are checking the difference between same field sys_created_on so change the second field backend name,

2.in subtract it should be (gd2 , gd1)  it should be ,(comma) not .(full stop)

 

i have modified the code please check,

BR 

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var gd1 = new GlideDateTime(current.sys_created_on);
    var gd2 = new GlideDateTime(current.sys_updated_on);
    var duration = GlideDateTime.subtract(gd2, gd1);
    g_scratchpad.diff = duration.getDisplayValue();

})(current, previous);

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

View solution in original post

2 REPLIES 2

swathisarang98
Giga Sage
Giga Sage

Hi @Mark Wood ,

 

There are few errors please correct it,

 

1. you are checking the difference between same field sys_created_on so change the second field backend name,

2.in subtract it should be (gd2 , gd1)  it should be ,(comma) not .(full stop)

 

i have modified the code please check,

BR 

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var gd1 = new GlideDateTime(current.sys_created_on);
    var gd2 = new GlideDateTime(current.sys_updated_on);
    var duration = GlideDateTime.subtract(gd2, gd1);
    g_scratchpad.diff = duration.getDisplayValue();

})(current, previous);

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

@Mark Wood Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query

 

Thanks,

Swathi