Difference of two Dates
There may be a situation where you need to calculate the number of days between the current date, and a date picked by the user. We need to perform some conversions in order to calculate the difference in time.
Let's begin by creating all of the elements used for this demonstration. The form layout should go like this:
This captures the date the user enters in the "date1" element and converts into milliseconds from Jan1 1970. We perform the same conversion for the current date and then subtract the difference between the two.
In "date_difference" we take the difference in milliseconds and convert it back into days, fixing the output to calculate only two decimal places (that is what ".toFixed(2)" is for). We then need to check and make sure that if the answer we compute is greater than 0 but less than one, to display the answer as "1".
For any other value we are saying to round it to the nearest whole number by adding ".toFixed(0)" in the dynamic value for "display_difference".
Once you understand how these calculations and conversions are performed, you can modify this example to suit your needs.