Comparing difference between two Dates or Times

  • 20 August 2020
  • 3 replies

Userlevel 7
Badge +10

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.


Three inputs, dt1, dt2, and time_in_minutes

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:

The script does provide the difference in minutes and hours in case you want to use that elsewhere.


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"
var compare = "false"

output = [{"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. 

3 replies

Dude! Thanks for sharing. Wil try it and let you know.

It’s unbelievable that I cannot do a simple operation in Zapier as 11:00 - 09:00 = 02:00.

The Compare Dates output is frustrating as hell!


Beginner here. Really appreciate the simplicity if your code, its easy to understand and effective. But I am currently practicing conditions. Can this same comparison of two times be written with if and else, with the return of a time difference?