
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-14-2022 02:14 PM
Story
-------
Every now and then I encounter questions in the community regarding date comparison of some sort, very often the question is to find the difference in days between two dates, and often the answer is a script using various GlideDateTime functions.
Using Flow designer is no exception, there is also a need for date comparison there, and the answer is often using some inline scripting.
So in the spirit of Flow designer I've built an reusable action to make it easier to retrieve these values, and avoid scripting in numerous situations.
One specific scenario that I've also wanted to address is the problem when using if/else Flow logic, there is no way to make an if statement like the following; if (datetime1 is relative x days before datetime2)
Details
---------
-You can either input one or two datetime objects to the action. If only one input is submitted, the action assumes/calculates the difference between the given datetime, and the current system datetime.
-The action always returns a zero or positive integer values on the difference in days/hours/and so on (ex. no negative numbers if the start date is after the end date).
Sample usage image below, and the complete XML is attached.
Install steps
----------------
- Navigate to System Update Sets > Retrieved Update Sets.
- Click the Import Update Set from XML link.
- Click Choose File and select the Date_Comparison_Flow_Designer.xml file.
- Click Upload.
The update set appears in the Retrieved Update Set related list in the Loaded state.
- Click Preview Update Set.
The Update Set Preview page shows results and lists problems.
- Click Commit Update Set.
- 10,743 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Also available on Share.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Good work ! Thanks....

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you Sohail!

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you Justin,
Nice video! Really appreciated.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Super Useful!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great stuff! Thanks a bunch.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Brilliant! Greatly appreciated, thank you!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
Is it possible to check with this function if the Start date of the Trigger = a day of the month without importance for the year.
Example: If Triggers = February 15 (No matter the year) Then create task
In the objective to create a task in Flow Designer every year on a fixed date
Thank

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @V_ronique C_t_
No, this tool is not supposed to evaluate if it is a specific date. It's only used to calculate the difference in time between two dates.
For your use case, I would recommend triggering the Flow monthly, and set it to that specific date of your requirement, and if needed only to run once a year, do a simple check as the first step, to verify if the month is the specific month when you want to run the Flow.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
I tried to make a check condition on the Trigger but I can't get any text. I seem to need two variables.
Do you have a Exemple ?
Thank

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Check this article by Mark Roethof, you can use it as a template, and do some similar action that checks if it's the correct month.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks a bunch @OlaN , works perfectly!

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you for the feedback @Brandon J
Ï'm glad it helped you. 😀
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for writing this. However, I was hoping that I can use it to both compute the remaining days to a set date AND find out whether the set date is in the future or in the past. Unfortunately, this is not the case, as your code always returns a positive number (some absolute value, of sorts).
Can you help here? I am interested to get "0" if the set date is in the present or in the past, or a positive number if it is in the future.
Thanks for any help.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you @ciacob2
The tool fills a very specific spot, sure.
For your scenario I guess you could use the standard if-condition in Flow designer, if you only want to find out if a date is before or after another date.
Like the example provided below.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Indeed, that is correct. However, I found that, with a minimal change in the code, returned numeric values can report positive values for the future and negative values for the past (and `0` for the precise "present" moment).
I leave the code here, if anyone should find it useful. In order to use it, one will make a copy of your Action and substitute existing code for the one below:
(function execute(inputs, outputs) {
try {
var startingPoint = new GlideDateTime(inputs.start_date);
var endingPoint = new GlideDateTime();
if (inputs.end_date) {
endingPoint.setValue(inputs.end_date);
}
var startValue = startingPoint.getNumericValue();
var endValue = endingPoint.getNumericValue();
var difference = endValue - startValue; // difference in milliseconds
outputs.difference_minutes = parseInt(difference / (1000 * 60));
outputs.difference_hours = parseInt(difference / (1000 * 60 * 60));
outputs.difference_days = parseInt(difference / (1000 * 60 * 60 * 24));
var weekCounter = 0;
var temporaryDateWeek = new GlideDateTime(startingPoint);
while (temporaryDateWeek.before(endingPoint) || temporaryDateWeek.equals(endingPoint)) {
temporaryDateWeek.addDaysUTC(7);
weekCounter++;
}
outputs.difference_weeks = startingPoint.before(endingPoint) ? weekCounter : -weekCounter;
var monthCounter = 0;
var temporaryDateMonth = new GlideDateTime(startingPoint);
while (temporaryDateMonth.before(endingPoint) || temporaryDateMonth.equals(endingPoint)) {
temporaryDateMonth.addMonthsUTC(1);
monthCounter++;
}
outputs.difference_months = startingPoint.before(endingPoint) ? monthCounter : -monthCounter;
var yearCounter = 0;
var temporaryDateYear = new GlideDateTime(startingPoint);
while (temporaryDateYear.before(endingPoint) || temporaryDateYear.equals(endingPoint)) {
temporaryDateYear.addYearsUTC(1);
yearCounter++;
}
outputs.difference_years = startingPoint.before(endingPoint) ? yearCounter : -yearCounter;
} catch (err) {
gs.addErrorMessage('Error occurred in date comparison: ' + err.message);
}
})(inputs, outputs);