2 Replies Latest reply on Jun 1, 2012 11:28 AM by Smitch1581

# Calculating date fields

Hi

I have three fields. 2 are formatted with the date field, and the third is for calculating the difference between the 2 date fields.

For example, field1 says Aug 2012, and field 2 says Sep 2012. Is there a way i can make field 3 dipslay the answer 1(month)?

Any help would be greatly appreciated.

Thank you

• ###### 1. Re: Calculating date fields

Yes, you use the JavaScript date object to extract the full year and month from the entry fields, create a lowest common denominator for the month and year, and do the math.

The custom calculation event for the number of months:

// clear the result value

event.value = "";

// get field values

var sStart = this.getField("Date1").value;

var sEnd = this.getField("Date2").value;

// process only if we have data

if(sStart != "" && sEnd != "") {

// convert date strings to date objects

var oStart = util.scand("mmm yyyy", sStart);

var oEnd = util.scand("mmm yyyy", sEnd);

// get the start date full year and month

var nStartFullYear = oStart.getFullYear();

var nStartMonth = oStart.getMonth();

// convert start data to months

var nStartMonths = (nStartFullYear * 12) + nStartMonth;

// get the end date full year and month

var nEndFullYear = oEnd.getFullYear();

var nEndMonth = oEnd.getMonth();

// convert end data to months

var nEndMonths = (nEndFullYear * 12) + nEndMonth;

// do the math

event.value = nEndMonths - nStartMonths;

} // end have data

You will have to adjust the names for the start and end dates.

• ###### 2. Re: Calculating date fields

Genius! Thank you very much, that works a treat.

Thank you