OlaN
Giga Sage
Giga Sage

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.

find_real_file.png

find_real_file.png

 

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.

 

Comments
OlaN
Giga Sage
Giga Sage

Also available on Share.

 

Sohail Khilji
Kilo Patron
Kilo Patron

Good work ! Thanks....

OlaN
Giga Sage
Giga Sage

Thank you Sohail!

Justin Meadows
Kilo Guru

@OlaN I gave you a shout out on my YT channel. I just happened to record a video the same day you posted this here. My other video (Script in Flow Designer, Like a Boss!) was trying to emphasize moving scripts into actions so they can be repurposed and reused. When you published this I decided to make another video (below) showing this date comparison action. Nice work!

OlaN
Giga Sage
Giga Sage

Thank you Justin,

Nice video! Really appreciated.

User658880
Tera Expert

Super Useful!

Lon Landry4
Mega Sage

Great stuff! Thanks a bunch.

Trevor Petrie
Giga Guru

Brilliant! Greatly appreciated, thank you!

V_ronique C_t_
Tera Contributor

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

OlaN
Giga Sage
Giga Sage

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.

V_ronique C_t_
Tera Contributor

Hi, 

 

I tried to make a check condition on the Trigger but I can't get any text. I seem to need two variables.

 

V_roniqueC_t__1-1689195081951.png

  Do you have a Exemple ? 

 

Thank

OlaN
Giga Sage
Giga Sage

@V_ronique C_t_ 

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.

Brandon J
Tera Contributor

Thanks a bunch @OlaN , works perfectly!

OlaN
Giga Sage
Giga Sage

Thank you for the feedback @Brandon J 

Ï'm glad it helped you. 😀

ciacob2
Tera Contributor

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.

OlaN
Giga Sage
Giga Sage

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.

compare-dates.png

ciacob2
Tera Contributor

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);
Version history
Last update:
‎05-14-2022 02:14 PM
Updated by: