
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
07-23-2018 09:44 AM - edited 11-05-2023 03:44 PM
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.
So I get asked this question a lot: The GlideDateTime DateDiff functionality does not seem to work like I want. Is there another method for obtaining a difference?
The answer is Yes!
The solution actually utilizes GlideDateTime functions to obtain the answer; which I encapsulated in a function to simplify matters.
I created the following function as an example. It simply takes a GlideDateTime value passed-in as a parameter, converts that to milliseconds, and then subtracts that from the current date/time (in milliseconds), and returns the result in days. You can make this more generic if needed, or perhaps pass a second date/time value in to get a diff between two different dates (not just the current date).
const DAY = 24*60*60*1000; // milliseconds in a day
// dateTimeValue = GlideDateTime
// tzOffSet = boolean (true/false)
function Time_Difference(dateTimeValue, tzOffSet) {
// apply timezone offset
var offSet = tzOffSet ? dateTimeValue.getTZOffset() : 0.0000;
// get the milliseconds value of the passed-in date/time
// assumption: if timezone offset chosen then this value is in local date/time
var dateTimeMilli = dateTimeValue.getNumericValue();
// get the current date/time value and convert it to milliseconds
var gtNowMilli = new GlideDateTime().getNumericValue() + offSet;
// now return the diff and pass it back converted to days
return (gtNowMilli - dateTimeMilli) / DAY;
}
A usage for this function might be to check the number of days between a date and today. That would look something like this:
const DAY = 24*60*60*1000; // milliseconds in a day
var date_time_value = new GlideDateTime('2023-07-04 03:58:12');
gs.info('---> date_time_value: ' + date_time_value);
var diff = Time_Difference(date_time_value, false);
// time zone adjustment of original date
var offSetTZ = date_time_value.getTZOffset();
date_time_value.setNumericValue(date_time_value.getNumericValue() + offSetTZ);
gs.info('---> dateTimeValueTZ: ' + date_time_value);
var diffTZ = Time_Difference(date_time_value, true);
// results
gs.info('---> diff in days:' + diff);
gs.info('---> diffTZ in days:' + diffTZ);
// end test
function Time_Difference(dateTimeValue, tzOffSet) {
var offSet = tzOffSet ? dateTimeValue.getTZOffset() : 0.0000;
var dateTimeMilli = dateTimeValue.getNumericValue();
var gtNowMilli = new GlideDateTime().getNumericValue() + offSet;
return (gtNowMilli - dateTimeMilli) / DAY;
}
Which would give the following result:
*** Script: ---> date_time_value: 2023-07-04 03:58:12
*** Script: ---> dateTimeValueTZ: 2023-07-03 20:58:12
*** Script: ---> diff in days:124.82145049768519
*** Script: ---> diffTZ in days:124.82145050925926
Try it out in Scripts - Background! Play around with the date value and see the difference. You will find this is pretty acccurate.
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 07-23-2018 11:44 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 735 Views