count of months between 2 dates
Need number of months between 2 dates
I'm trying this script for 2 date fields... do the date fields have to be in a specific date format for this to work? Currently set to dd-mmm-yy
function Scand(cFormat, cString) {
// convert a date or time string to a date object;
var oDate = util.scand(cFormat, cString);
if(oDate == null) app.alert("Error converting " + cString + " with format of " + cFormat, 0, 0);
return oDate;
}
var cDateFormat = "dd/mm/yyyy"; // format for date string;
// Forecast values;
var cForecast = "20/02/2014";
var oForecast = Scand(cDateFormat, cForecast);
// Expiry values;
var cExpiry = "20/03/2015";
var oExpiry = Scand(cDateFormat, cExpiry);
// check that Expiry is not before the Forecast;
if(oExpiry.getTime() < oForecast.getTime()){
app.alert("Expiry before Forecast!", 0, 0);
} else {
// compute the differences;
var nDiffYears = oExpiry.getFullYear() - oForecast.getFullYear(); // diff in years;
// difference in years * 12 months/year + difference in months;
var nDiffMonths = (nDiffYears * 12) + (oExpiry.getMonth() - oForecast.getMonth());
}
// convert year to months and add to months;
event.value = (nDiffYears * 12) + nDiffMonths;
