Best answer

What's the best way to find the number of days between 2 dates?

  • 12 July 2019
  • 5 replies
  • 3527 views

Userlevel 7
Badge +12

I have a Zap that has start date and a due date and I'dl like to be able to find the number of days there are in the project. In other words, how many days are between the start date and the end date.

I feel like this is something that should be doable with the Formatter by Zapier app, but I can't quite figure it out. Any ideas?



icon

Best answer by jesse 3 May 2022, 18:01

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Userlevel 7
Badge +9

Hey Danvers! This is totally possible.

Option 1

You can use Google Sheets to calculate the number of days AND to format the dates for you.

Here is how you can set this up within your Zap:

The sheet has a column for the later date, for the earlier date and then one with a formula. Map the dates for the formula right inside the Zap (earlier date, comma, later date).

Notice that I get the data returned by the step (so I can use it in later steps) and I also get the rows added to my spreadsheet. Also, note that one of my dates was formatted and the other wasn't. That's because I changed the formatting for the column in Google Sheets:


So you select the entire column, navigate to what's shown above and create the format that you want. Now, I can send that data somewhere else.

Option 2

1) Another way is to convert the UNIX timestamps into MM/DD/YYYY format with Formatter steps. Then, add a Formatter step using "Numbers" > "Perform Math Operation" > "Subtract" to subtract the later date from the earlier date: 

2) Add another Formatter step using "Utilities" > "Spreadsheet-Style Formula" to dive the results by total seconds of day, then round it to the nearest 0 decimal: 

3) This provides an output that's the number of days between the UNIX timestamps: 

Hope that helps! Also curious to hear how others have approached this and if there is a better way.


Userlevel 1

image.pngYou can use this python code as well


Userlevel 7
Badge +10

@Mews_Zapier - thanks. Any chance you can inline that code for an easy copy/paste experience? 😅


Userlevel 1

Oh sry, sure,

so in input data at the top (formated as dd/mm/yyyy):

dat1 = {{newer date}}

dat2 = {{older date}}


and then the python code:


import datetime

from datetime import timedelta


datetimeFormat = '%d/%m/%Y'

diff = datetime.datetime.strptime(input_data['dat1'], datetimeFormat)\

  - datetime.datetime.strptime(input_data['dat2'], datetimeFormat)


output = print(diff.days)



Userlevel 7
Badge +9

Since my last post, we’ve created a better way to do this! The Formatter app now includes a “Compare Dates” Transform that makes this process super simple. You can learn more on exactly how to set this up, here: https://zapier.com/help/create/customize/compare-dates-and-times-in-zaps