Skip to main content
gerrye50334270
Known Participant
May 9, 2019
Answered

count of months between 2 dates

  • May 9, 2019
  • 2 replies
  • 2865 views

 

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;

This topic has been closed for replies.
Correct answer try67

No, it needs to be a calculation script.

2 replies

gerrye50334270
Known Participant
May 9, 2019

...I guess I should clarify my ask -- need it to count the 'active' months between two dates.

Bernd Alheit
Community Expert
Community Expert
May 9, 2019

For cDateFormat use the format of the date fields.

gerrye50334270
Known Participant
May 9, 2019

K - did that but I'm clearly still missing something  ... I put the script under the "Validation" tab in the Text Field Properties -- is that the right place? for it

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-mmm-yy"; // format for date string;

// Forecast values;

var cForecast = "20-Feb-14";

var oForecast = Scand(cDateFormat, cForecast);

// Expiry values;

var cExpiry = "20-Mar-15";

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;

Bernd Alheit
Community Expert
Community Expert
May 9, 2019

Sorry - lost.... am I changing the formula/script or am I changing the 2 date fields (Forecast, Expiry)?  If so - to what?


Why does you use in the code fix values?