JavaScript:获得平均花费的时间

时间:2018-06-11 08:47:41

标签: javascript date time servicenow

我编写了一个脚本(工作)来获取两个日期之间的天数。 [的ServiceNow]

var chg = new GlideRecord("change_request");
chg.addEncodedQuery("assignment_group=1234^ORassignment_group=12345^sys_created_on>javascript:gs.dateGenerate('2017-06-30','23:59:59')^state=3");
chg.query();

var x = 0;

while(chg.next()) {

  x++;

  var start = new GlideDateTime(chg.sys_created_on.toString());
  var end = new GlideDateTime(chg.closed_at.toString());

  var dur = GlideDateTime.subtract(start, end);
  gs.addInfoMessage(x + " - " + chg.number + ": " + dur.getDisplayValue());

}

这会得到以下输出:

1 - CHG0000001: 29 Days 12 Hours 43 Minutes 
2 - CHG0000002: 3 Hours 20 Minutes 
3 - CHG0000003: 41 Minutes 
4 - CHG0000004: 78 Days 3 Hours 41 Minutes 
5 - CHG0000005: 22 Days 2 Hours 7 Minutes 
... 
...

变量:

x = 1
chg.number = CHG0000001
dur.getDisplayValue() = 29 Days 12 Hours 43 Minutes

如何使用所有条目获得平均时间?我迷路了。

1 个答案:

答案 0 :(得分:0)

感谢Isaac我可以这样解决:

var milisec = 0;
while(chg.next()) {

  x++;

  var start = new GlideDateTime(chg.sys_created_on.toString());
  var end = new GlideDateTime(chg.closed_at.toString());

  var dur = GlideDateTime.subtract(start, end);

  milisec = milisec + dur.getNumericValue(); //time spent in milliseconds

}

gs.addInfoMessage(((milisec/x)*1.1574074074067E-8)); //format milliseconds to days and get average