diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md new file mode 100644 index 0000000000..65693b6c3b --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md @@ -0,0 +1,23 @@ +**Usecase**: +This piece of code can be used to check whether a record was created more than 90 days ago or not. +It returns an output of true/false as well as the actual age (in days) of the record. + +**Type**: +Background Script + +**How it works**: +There is a pastDateString variable which can contain a date-time which you received by querying any particular record's sys_created_on field or any other relevant field. +In the actual code I have hard-coded a value to be used as an example. +-There are two gliderecord objects to store the current date as well as past date +-GlideDateTime.subtract() is used to calculate the time difference as a GlideDuration object +-The time duration in milliseconds is converted to days by dividing with 86400000(milliseconds in a day) +-Comparison is done between the results +-The final age of the record and true/false result of the comparison is the output. +[Note: Output may vary according to timezones] + +**Example**: +pastDateString = 2025-05-01 10:00:00 +Output: + + Record Age (Days): 165 + Older than 90 days? true diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js new file mode 100644 index 0000000000..958f10069c --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js @@ -0,0 +1,20 @@ +//the pastDateString variable can contain a date-time which you received by querying any particular record's sys_created_on field or any other relevant field. Here I'm hard-coding a value. + +var pastDateString = '2025-05-01 10:00:00'; //input date and time +var ageThresholdDays = 90; + +var gdtPast = new GlideDateTime(pastDateString); +var gdtNow = new GlideDateTime(); +var duration = GlideDateTime.subtract(gdtPast, gdtNow); +var durationMs = duration.getNumericValue(); + +//Calculate the total days (1 day = 86,400,000 milliseconds) +var totalDaysOld = Math.floor(durationMs / 86400000); + +var isOlderThanThreshold = false; +if (totalDaysOld > ageThresholdDays) { + isOlderThanThreshold = true; +} + +gs.info("Record Age (Days): " + totalDaysOld); +gs.info("Older than " + ageThresholdDays + " days? " + isOlderThanThreshold);