Problem
We often get use cases where we want the difference between today’s date and a past or future date. Current transforms in IdentityNow do not give us the difference between 2 dates in days. There are workarounds to convert date into epoch, calculate the difference, and convert that number back into days, but those are hacky and not clean.
Diagnosis
The purpose of this knowledge article is not just to share this simple rule but also to start a conversation about whether Sailpoint Professional Services should just build a standard rule for all tenants that can be used by all customers so that all those repetitive hacky workarounds are not needed.
Solution
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd"> <Rule name="DaysDifferenceGeneric" type="Transform"> <Description>Gets date input from transform and returns days diffrence to today's date</Description> <Source><![CDATA[ import java.util.Date; import sailpoint.object.Identity; import java.text.ParseException; import java.text.SimpleDateFormat; log.error("Inside-DaysDifferenceGeneric-rule: started"); String lastLogin=date; if(lastLogin == null) { log.error("Inside-DaysDifferenceGeneric-rule: lastlogin is null"); return null; } log.error("Inside-DaysDifferenceGeneric-rule: lastlogin value in string "+lastLogin ); try { // Define the date format SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); // Parse the input string into a Date object Date staticDate = isoFormat.parse(lastLogin); log.error("Inside-DaysDifferenceGeneric-rule: lastlogin value in date format "+staticDate ); // Current date Date currentDate = new Date(); // Calculate the difference in milliseconds long diffInMillies = Math.abs(currentDate.getTime() - staticDate.getTime()); // Calculate different // Calculate different time units long diffInDays = diffInMillies / (1000 * 60 * 60 * 24); return String.valueOf(diffInDays); } catch (ParseException e) { throw new Exception("Invalid date format. Please use ISO 8601 format."); } ]]></Source> </Rule>