最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date comparison inside ServiceNow - Stack Overflow

programmeradmin6浏览0评论

When paring two dates, how do I get the difference between two date objects in DAYS? (for example - 2017-07-31 and 2017-07-28 --> this should return 3 days).

UNIX timestamp will not work as it is not accurate for my case - there is no way knowing whether I should round up or down to the nearest 24hours (1 day).

getDay() function does not work inside of scoped application within ServiceNow.

When paring two dates, how do I get the difference between two date objects in DAYS? (for example - 2017-07-31 and 2017-07-28 --> this should return 3 days).

UNIX timestamp will not work as it is not accurate for my case - there is no way knowing whether I should round up or down to the nearest 24hours (1 day).

getDay() function does not work inside of scoped application within ServiceNow.

Share Improve this question edited Feb 3, 2019 at 23:28 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 10, 2019 at 12:46 user5178035user5178035 9
  • can you use moment js in your project? – nmbrphi Commented Jan 10, 2019 at 12:52
  • no. You cannot load any libraries into ServiceNow. – user5178035 Commented Jan 10, 2019 at 12:54
  • if you can only use vanilla js, maybe you can use this: var diffDays = Math.floor(( start - end ) / 86400000); Where start and end are two dates objects. – nmbrphi Commented Jan 10, 2019 at 13:01
  • can you use your custom js or not – Ramiz Web Dev Commented Jan 10, 2019 at 13:04
  • No. only vanilla js or servicenow js (whoever is familiar with the platform). – user5178035 Commented Jan 10, 2019 at 13:06
 |  Show 4 more ments

3 Answers 3

Reset to default 2

The most monly used API available in Service Now to calculate duration is gs.DateDiff(date1, date2) but it does not work in scoped applications.

SN has provided its own APIs which can fetch the duration in a scoped app using GlideDateTime API

If your fields are date/time

var date1 = new GlideDateTime("2011-08-28 09:00:00");
var date2 = new GlideDateTime("2011-08-31 08:00:00");
var diff = GlideDateTime.subtract(date1, date2);
gs.info(diff.getDisplayValue());

If you need to refer it on any fields from a record then specify dates as

var date1 = new GlideDateTime(current.sys_created_on);
var date2 = new GlideDateTime(current.sys_updated_on);

To get the numeric difference in seconds

gs.info(diff.getNumericValue());

To subtract the duration for date fields only

diff = GlideDate.subtract(date1, date2);
gs.info(diff.getDisplayValue());

Given dates in the format YYYY-MM-DD, you can use the Date constructor, subtract one from the other and divide by the number of milliseconds in one day.

The strings will be parsed as UTC, so there are no issues with daylight saving and ECMAScript has exactly 8.64e7 ms per day. Even if you need to round, daylight saving doesn't change the value by more than 1 hour (and in some places less) so rounding is fine. E.g.

var d0 = '2017-07-31';
var d1 = '2017-07-28';
var diffDays = (new Date(d0) - new Date(d1)) / 8.64e7;

console.log(diffDays);

It's generally remended to avoid the built–in parser, however in this case it's likely OK. But for confidence, parse the strings manually, a library can help but it only requires a 2 line function.

If your dates are not created as above and are not set to midnight, you can get days between dates by setting them to midnight, subtracting and rounding, e.g.

var d0 = new Date(2017, 6, 31, 23, 15, 45); // 2017-07-31T23:15:25 local
var d1 = new Date(2017, 6, 28, 3, 15, 45);  // 2017-07-28T03:15:25 local

function diffDays(d0, d1) {
  var a = new Date(d0).setHours(0,0,0,0);
  var b = new Date(d1).setHours(0,0,0,0);
  return Math.round((a - b) / 8.64e7);
}

console.log(diffDays(d0, d1));

If you want to do this in say a business rule, inside a scoped application, it might look like the following (Tested in Orlando version):

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

    var field_1 = new GlideDateTime(current.field_1);
    var field_2 = new GlideDateTime(current.field_2);
    current.field_duration = new GlideDuration(field_1.getNumericValue() - field_2.getNumericValue());

})(current, previous);
发布评论

评论列表(0)

  1. 暂无评论