How to Calculate in Both Years and Months with Gravity Forms

Learn how to use Gravity Forms to calculate in both years and months for countdowns, job histories, exact ages, or anything else.

This article requires GP Date Time Calculator and GP Advanced Calculations.

Buy Gravity Perks today and get 47+ premium Gravity Forms plugins, including these perks!

Ever needed to use Gravity Forms to calculate a period of time in both years and months?

This is useful when calculating job histories, children’s ages, a countdown, or anywhere that you need an up-to-the-month calculation.

In this tutorial, we’re going to use Gravity Forms Date Time Calculator and Gravity Forms Advanced Calculations to count the number of years and months between two dates.

Steps

  1. Add fields to your form
  2. Years calculation
  3. Months calculation

Step 1 – Add fields to your form

Start by adding two Date fields and two Number fields to the form. Call the Date fields “Start Date” and “End Date”. Name the Number fields “Years” and “Months”.

gravity forms add fields to your form

Step 2 – Years calculation

With the fields in place, we’ll start with the years calculation. Click Enable calculation in the “Years” field settings, and subtract “End Date” from “Start Date”. Wrap that calculation in parentheses, and add floor in front of the opening parenthesis.

The floor function is included with Advanced Calculations and automatically rounds a number down to the nearest integer. Your final calculation should look like this:

floor( {End Date:3} - {Start Date:1} )

Next, set Rounding to 0 and Date Calculation Unit to Years. Here’s what the whole setup looks like:

calculate years in gravity forms

For our more frugal readers, an alternative for calculating the mathematical floor is to subtract 0.5 from the result and set the Rounding to 0. The calculation looks like this: ( {End Date:3} – {Start Date:1} ) – 0.5

Step 3 – Months calculation

The months calculation is similar to the years calculation, however we’re going to subtract the years calculation results to only return the remainder of months.

floor( {End Date:3} - {Start Date:1}  ) - ( {Years:4} * 12 )

Notice we multiply the {Years:4} merge tag by 12. This converts it to months. Same as before, set Rounding to 0. For this field, the Date Calculation Unit is Months.

gravity forms calculate months

That’s It

With the calculations in place, our form now calculates years and months together. Thanks to the floor functions, our calculations are accurate and delivered in whole numbers.

gravity forms calculate years and months

Questions?

Have questions or comments about this tutorial? We’d love to hear them! 👇

Comments

  1. Nurelegn Bayih
    Nurelegn Bayih April 27, 2023 at 3:55 pm

    I would like to buy the unlimited perks, My question is when I unsubscribe, will I loose all what I did with the plugins or just support and updates? Also is there a way to do sum of a column in a field on conditions, like if Field 1 is A then accumulate Field 2 and pupulate Field 3

    Reply
    1. Scott Ryer
      Scott Ryer Staff April 27, 2023 at 4:22 pm

      Hi Nurelegn,

      If your license expires, the plugins will still function. You will no longer be able to manage or update your Perks, but they’ll continue to work as long as they are installed and active.

      Regarding your second question, I think we’ll need some more information to help with that. Can you drop us a line so our Support Wizards can assist?

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Grab a bundle of free Gravity Forms plugins

Enter your email and receive our most popular free plugins and snippets, plus access to hundreds of others.

This field is for validation purposes and should be left unchanged.