How to calculate the SUM of a datediff?

MR Carvalho
Tera Contributor

Hi guys,

 

I did a datediff and I need to SUM the result, I tried the code below but is not working.

Could someone help me?

var incidentGA = new GlideAggregate('incident');

incidentGA.query();

 

while (incidentGA.next()) {

var date1 = new GlideDateTime(incidentGA.sys_created_on);
var date2 = new GlideDateTime(incidentGA.resolved_at);

diff = GlideDate.subtract(date1, date2);

var total = incidentGA.addAggregate('SUM', diff);

gs.info(diff.getDisplayValue());
gs.info(total);
}

 

Thanks in advance.

1 ACCEPTED SOLUTION

rishabh katari1
Mega Guru

Hi @MR Carvalho ,

As far as I can relate to the question you want to calculate the sum of differences in "TOTAL" variable.

 

Here I have tried this code, and its working fine.

 

 

var total=new GlideDateTime();
total.subtract(total.getNumericValue()); //sets time to zero
gs.print(total);
var incidentGA = new GlideAggregate('incident');
incidentGA.query();
while (incidentGA.next()) {
var date1 = new GlideDateTime(incidentGA.sys_created_on);
var date2 = new GlideDateTime(incidentGA.resolved_at);

diff = GlideDate.subtract(date1, date2);

total.add(diff);
}
gs.print(total);

Let me know if anything more is there to this.

 

Regards,

Rishabh

 

View solution in original post

3 REPLIES 3

rishabh katari1
Mega Guru

Hi @MR Carvalho ,

As far as I can relate to the question you want to calculate the sum of differences in "TOTAL" variable.

 

Here I have tried this code, and its working fine.

 

 

var total=new GlideDateTime();
total.subtract(total.getNumericValue()); //sets time to zero
gs.print(total);
var incidentGA = new GlideAggregate('incident');
incidentGA.query();
while (incidentGA.next()) {
var date1 = new GlideDateTime(incidentGA.sys_created_on);
var date2 = new GlideDateTime(incidentGA.resolved_at);

diff = GlideDate.subtract(date1, date2);

total.add(diff);
}
gs.print(total);

Let me know if anything more is there to this.

 

Regards,

Rishabh

 

MR Carvalho
Tera Contributor

@rishabh katari1 thank you very much.