This guide shows some useful tips when performing date calculations using the Date Picker form element and the Form Calculation widget. It’s important that you’re comfortable working with Form Calculation. To learn more, see Form Calculation.
- Form Calculation converts dates to sequential serial numbers (datetime) so they can be used in calculations. By default, 1970-01-01 (00:00:00 UTC) is 0, and 2095-01-23 (12:00:00 UTC) is 45678.50 because it is 45,678 days and 12 hours from January 1, 1970 midnight.
- Form fields added in Form Calculation must be set to Number, indicated by the [0-9] label, for the calculations to work.
Adding or Subtracting Days to a Date
You can directly add or subtract days to a date in Form Calculation. The result will be a sequential serial number representing a date. To convert it back to a readable date format, use the dateString() function.
|Formula||dateString(Date + Days)|
|Result||Sun Jan 06 2030|
You can also add a specified number of units to a particular date in Jotform Tables using the DATEADD function. To learn more, see Advanced Formula: Date Functions.
Getting the Difference Between Two Dates
To get the difference between two dates, subtract one Date Picker from another. The result will be the number of days, in decimals, between the two dates.
|Formula||To – From|
To get the absolute result regardless of the dates’ order, use abs() in Form Calculation. You can also get the difference between two dates in your specified units in Jotform Tables using the DATETIME_DIFF function. To learn more, see Advanced Formula: Date Functions.
Converting Days to a Different Unit of Time
Date calculations in the Form Calculation widget return the result in days units. To turn it, or any number, into a different unit of time, add a Form Calculation widget to your form and set the unit’s formula from the following table:
|Years||Days / 365|
|Months||Days / 30|
|Hours||Days * 24|
|Minutes||Days * 1440|
|Seconds||Days * 86400|
Note: Rounded conversion factors are used for years and months. For other values, see Conversion Factors: Time.
Converting Days to Years, Months, and Days
Supposing, you have the number of days, and now, you want to convert this number to a years, months, and days format. To set it up, add three Form Calculation widgets to your form and use the following formula for each:
|Years||floor(Days / 365)|
|Months||floor((Days % 365) / 30)|
|Days||floor(Days % 365 % 30)|
- Rounded conversion factors are used for years (365) and months (30). For other values, see Conversion Factors: Time.
- To use the modulo operator, use your keyboard to type in the percent (“%”) symbol in Form Calculation. The modulo operation returns the remainder of a division.
- The floor() function rounds the result down to the nearest integer.
- You can test the results in this demo form.