Solution for adding months to a date (native Knack features)

I recently had the need to add months (1, 2 or 6) to a date.

As per previous topics there is no obvious way to do this in Knack, due to the complexities of different length months and leap years, and the absence of a function to add months to a date.

Some examples of what we are trying to achieve are below:

15 Jan 2022 plus 1 month should be 15 Feb 2022
31 Jan 2022 plus 1 month should be 28 Feb 2022
31 Jan 2024 (leap year) plus 1 month should be 29 Feb 2022

There’s a surprising way to do this in Knack that I discovered by chance today. Explained below.

On average, 1 month is 0.0833333 years (1 month divided by 12 months = 0.08333…). This is an inaccurate average that does not account for different length months and leap years, but it doesn’t seem to matter in this workaround.

To set this up we need 3 fields:

  • Initial date (date field)
  • Years to add (number field)
  • Final date (equation field)

The 3 fields are set up as per the 3 images below:

Now, we set our “Years to add” field to 0.083333333 (1 month).

And we find that it calculates the final date successfully.

Straightforward example:
image

Complex examples:
image

image

This can of course be extended with more complex logic, eg methods to autofill the “Years to add” field with different values based on a user selection etc. This is just the basic concept.

It also works with other values above 1 month in duration.

eg adding 3 months: we change our “Years to add” field value to 0.25 and it works as expected still:

image

image

13 Likes

Hi

I definitely need this for my loan management system app. I can then be able to show both start and end dates in the Loan Agreement. Example: A client get approved for a loan with repayment term of 24 months on 28 June 2022 ( By this means 28 June 2022 + 24 = 28 June 2024), I am right?

This is fantastic! Thank you so much for taking the time to share this solution!

Correct

Callum,
thank you so much for this solution! Was exactly what i needed at this moment.
I needed to add N months and then fast-forward to the last day of that month, so I added a Formula field that took your result and enclosed in getDateLastDayOfMonth({…}).
it’s golden!!

Thanks @Callum.Boase - just had the need to do exactly this. A great workaround :+1:
Saved me a whole load of work.

Thanks for posting this it really helped us out.

Craig

I repeated what you did but I don`t get any result like yours. Has something changed in Knack? My issue is similar, I want to calculate a date 40 days before a given date - should be straightforward do-able with your workaround but I can not get it to work ;-(

image

image

Hi
Is your days to deduct a number field?

Craig

Hi Craig, appreciate your lightning fast response, Yes it is.

You will need to make sure it has 10 decimal places for the number field.

Craig

Or it will round it down to 0.

Wouldn’t it be simpler, in this instance because the unit of time is days and not months, to just subtract 40 (days) from the Pick Up Date?

Equation type = Date
Date Type = Days
Result Type = Date

Yes you are right. I should have re read his post. The original post was about adding months which for some reason Knack doesn’t allow for. Yes of you want to minus days this can be done natively in knack.

Thanks guys - I was too occupied by what I read earlier ( Knack can not simply add month to a date) to try the obvious first. Was looking for a workaround that is not needed.
Thanks for your comments!
Cheers, Ulf

@Callum.Boase Needed this one today! Thanks!

1 Like