 # Subtract Dates from each other

Hi,

Not sure if this has been brought up before, but looking to create a snippet where I am able to subtract days from each other. I found this topic but it's not quiet what I'm looking for. (Subtracting months)

Here is the full idea for reference:
My team calculates how much to spend per day on Ad Campaigns. They usually apply a formula: Budget / (End Date - Start Date)

The idea would be to use 2 Form Inputs, enter the days, and then receive the difference in number of days. Then also have a Form Input to enter the budget, and return out the Spend Per Day. However, I am struggling to get the dates to subtract from each other.

2 Likes

Hi, and welcome to the forum! You can do this using the "X" format option for the {time} command. It converts the time/date to a timestamp which is the number of seconds since 1970 (this is a standard unit of time for computer systems). You can then use that to calculate the time between two dates.

For example:

Budget: \${formtext: name=budget; default=10000}
Start Date: {formtext: name=start; default=2020-01-01} (YYYY-MM-DD format)
End Date: {formtext: name=end; default=2020-01-31}

{days=({time: X; at={=end}} - {time: X; at={=start}}) / 24 / 60 / 60} (calculate difference in seconds and then convert to days)

Per day rate {=budget / days; format=\$,.2f}

As an aside, we're planning on launching a date selector in the coming months that will make entering a date simpler. You can take an early look at it here:

2 Likes

This is so cool! Thank you so much! Super helpful! I see the part of my snippet that I was struggling with so thank you. Going to be a huge improvement to productivity!

1 Like