Recently I needed to compare the difference between a couple of dates and I realized that formatter doesn’t really have a good way of doing this, so I wrote a simple javascript and tried to make it as generic as possible so others in the community can use it.
My use case is that we were getting notifications of a form submission from jotform days after the form was submitted and the zap was re-processing the form submission which is what I didn’t want to have, so I wanted a simple way to “test” if {{zap_meta_human_now}} was within 30 minutes of the timestamp on the form submission, if it’s greater than 30 minutes I don’t trust it.
First a caveat, this is simple code, not perfect, and doesn’t account for the various needs, but can be adjusted to accomplish a lot of needs.
In the screenshot I did not use {{zap_meta_human_now}} however, in production I am using that for dt2.
Javascript is pretty forgiving on the date/time format, so as long as it can understand then it’ll do it’s best.
I used time_in_minutes to return a true/false value so you can use a filter or a path to say if “greater than minutes” is true then do this, if false, do that.
Here’s an example output:
And finally the code, so you can copy/paste this into a code step for yourself.
var dt1 = new Date(inputData.dt1).getTime()
var dt2 = new Date(inputData.dt2).getTime()
var tim = inputData.time_in_minutes
var diff =(dt2 - dt1) / 1000;
var diffh = Math.abs(Math.round(diff/3600));
var diffm = Math.abs(Math.round(diff/60));
if (diffm > tim) {
var compare = "true"
}else{
var compare = "false"
}
output = m{"greater than minutes":compare,"setting for number of minutes":tim,"difference in minutes": diffm, "difference in hours": diffh,"dt1": dt1, "dt2": dt2}];
And for anyone who modifies/tweaks this code to their situation please post a copy of your code here so others can continue to benefit from this.
This is of course not perfect, but it met my needs and if it meets your needs I’ll be happy to know that. If you need something similar just comment below and I might be able to tweak the code to take your situation into account.