Skip navigation
Currently Being Moderated

Calculate Months from Field Date

Jul 24, 2012 1:16 PM

Tags: #date #since #months

Using: Acrobat X Pro in windows. 

 

I have three fields:

1) Today's Date:___________ [DateToday] -- Automatically filled-in with today's date using script.

2) Date Last Payment Made:__________ [LastPaymentMade1] -- Filled-in by user in mm/dd/yyyy format.

3) Months Since Last Payment:______________ [MoBehind] -- Need to calculate from field 1 - 2).

 

For #3, how do I custom calculate how many MONTHS it has been (#3) since the date the last payment was made (#2)?

 

There's a great discussion here on this topic but I cannot figure out how to modify the script for my purposes: http://forums.adobe.com/message/1907993#1907993

 

This appears to be the closest thing to what I want:

 

function Floor(fValue) { 
return Math.floor(fValue); 
} 
 
 
function Date2Num(sDate, sFormat) { 
var fSecond =1000; 
var fMinute = 60 * fSecond; 
var fHour = 60 * fMinute; 
var fDay = 24 * fHour; 
var oDate = util.scand(sFormat, sDate); 
var fDate = oDate.valueOf(); 
var fDate = fDate / fDay; 
var fDate = Floor(fDate); 
return fDate; 
} 
var Start = this.getField("DateToday").value; 
var sFormat = "mm/dd/yyyy"; 
var End = this.getField("LastPaymentMade1").value; 
var sFormat = "mm/dd/yyyy"; 
var eFormat = sFormat; 
event.value = Date2Num(End, eFormat) - Date2Num(Start, sFormat); 
 

 

Here's another one that looks SIMILAR to what i want:

 

 

(function () {
 
    // Get date from field
    var v = getField("DateToday").value;
 
    if (v) {
 
        // Convert string to date
        var d = util.scand('mm/dd/yyyy', v);
 
        // Add number of days in NUMDAYS field
        d.setDate(d.getDate() - +getField("NumDays").value);
 
        // Set value of this field to the new date
        event.value = util.printd("mm/dd/yyyy", d);
 
 
    } else {
 
        // Blank field if no date entered
        event.value = "";
    }
})();
 
 
Replies
  • Currently Being Moderated
    Jul 24, 2012 2:32 PM   in reply to Neil-Kentucky

    Since the number of days in a month are not constant across a year I do not think you can use the milliseconds in a day.

     

    I you use the 'getMonth()' and 'getFullYear()' methods for the calculation.

     

    // get the date values

    var Start = this.getField("DateToday").value;

    var sFormat = "mm/dd/yyyy";

    var End = this.getField("LastPaymentMade1").value;

    var sFormat = "mm/dd/yyyy";

    var eFormat = sFormat;

    // convert to date object

    var oStart = util.scand(sForamt, Start);

    var oEnd = util.scand(eFormat, End);

    // get start month

    var StartMonth = oStrat.getMonth();

    // add start full year as months

    StartMonth = StartMonth + (oStart.getFullYear() * 12);

    // get end month

    var EndMonth = oEnd.getMonth();

    // add end full year as months

    EndMonth = EndMonth + (oEnd.getFullYear() * 12);

    // compute difference in months

    var diffMonths = EndMonth - StratMonth;

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points