These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

calculating month, pls help~

andrewchow11
Registered: Mar 28 2011
Posts: 20

This javascript can get the total of the "DAYS" value
=============================================================
var strStart = this.getField("DateStart").value;
var strEnd = this.getField("DateEnd").value;
if(strStart.length && strEnd.length)
{
var dateStart = util.scand("mmm d, yyyy",strStart);
var dateEnd = util.scand("mmm d, yyyy",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
event.value = days;
}
else
event.value = 0;
=============================================================
 
How to change can get the total of the "MONTHS" value??
for example,
DateStart: 01/01/2000
DateEnd: 01/02/2001
result is 13
 
Plase help and thanks!!
 

My Product Information:
Acrobat 9.0
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
How many days or minutes in a month?

It is not consistent from month to month, so you can not use converting the dates to a time value and then compute.

With paper and pencil, one can convert the years to months and add that value to the months for each date to get the total month for each date. Then if one subtracts the total months for each data, one would have the number of complete months between the dates. Add 1 to include the ending month.


function Date2Months(cFormat, sDate) {
var nMonths = 0;
// convert date string to date object
var oDate = util.scand(cFormat, sDate.toString();
// get the years
var nYears = oDate.getFullYear();
// get then months
nMonths = oDate.getMonth();
// add the years in months and return the value
return nMonths + (12 * nYeaars);
} // end Date2Months

var strStart = this.getField("DateStart").valueAsString;
var strEnd = this.getField("DateEnd").valueAsString;
if(strStart.length && strEnd.length)
{
// get number of months for start date
var nStart = Date2Months("mmm dd, yyyy", strStart);
// get number or months for end date
var nEnd = Date2Months("mmm dd, yyyy", strEnd);
// compute difference
event.value = nEnd - nStart;
}
else
event.value = 0;


George Kaiser

andrewchow11
Registered: Mar 28 2011
Posts: 20
hello gkaiseril,

thanks for your reply, I did the following test,
(test1):
DateStart: 01/01/2000
DateEnd: 01/01/2001
(test2):
DateStart: 01/01/2000
DateEnd: 01/02/2000

but event.value why did not respond and display??

===============================================================
function Date2Months(cFormat, sDate) {
var nMonths = 0;
var oDate = util.scand(cFormat, sDate.toString());
var nYears = oDate.getFullYear();
nMonths = oDate.getMonth();
return nMonths + (12 * nYeaars);
}


var strStart = this.getField("DateStart").valueAsString;
var strEnd = this.getField("DateEnd").valueAsString;
if(strStart.length && strEnd.length)
{
var nStart = Date2Months("dd/mm/yyyy", strStart);
var nEnd = Date2Months("dd/mm/yyyy", strEnd);
event.value = nEnd - nStart;
}
else
event.value = 0;
===============================================================


gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Sorry, left off a closing parenthesis:

function Date2Months(cFormat, sDate) {
// convert date string to date object
var oDate = util.scand(cFormat, sDate.toString());
var nMonths = 0
// get the years
var nYears = oDate.getFullYear();
// get then months
nMonths = oDate.getMonth();
// add the years in months and return the value
return nMonths + (12 * nYears);
} // end Date2Months

var strStart = this.getField("DateStart").valueAsString;
var strEnd = this.getField("DateEnd").valueAsString;
if(strStart.length && strEnd.length)
{
// get number of months for start date
var nStart = Date2Months("mm/dd/yyyy", strStart);
// get number or months for end date
var nEnd = Date2Months("mmm dd, yyyy", strEnd);
// compute difference
event.value = nEnd - nStart;
}
else
event.value = 0;

George Kaiser

andrewchow11
Registered: Mar 28 2011
Posts: 20
hello gkaiseril, thanks a lot!! ^.^
andrewchow11
Registered: Mar 28 2011
Posts: 20
gkaiseril wrote:
Sorry, left off a closing parenthesis:function Date2Months(cFormat, sDate) {
// convert date string to date object
var oDate = util.scand(cFormat, sDate.toString());
var nMonths = 0
// get the years
var nYears = oDate.getFullYear();
// get then months
nMonths = oDate.getMonth();
// add the years in months and return the value
return nMonths + (12 * nYears);
} // end Date2Months

var strStart = this.getField("DateStart").valueAsString;
var strEnd = this.getField("DateEnd").valueAsString;
if(strStart.length && strEnd.length)
{
// get number of months for start date
var nStart = Date2Months("mm/dd/yyyy", strStart);
// get number or months for end date
var nEnd = Date2Months("mmm dd, yyyy", strEnd);
// compute difference
event.value = nEnd - nStart;
}
else
event.value = 0;
hi gkaiseril, thanks a lot~
I testing this script, if "both dates inclusive - Start and end days are included":
Start:01/01/2000 End:30/01/2000 (show result=0,correct)
Start:01/01/2000 End:31/01/2000 (show result=0,but correct result is 1)
Start:01/01/2000 End:31/12/2000 (show result=11,but correct result is 12)

How to solve it? please help and thanks!