Expand my Community achievements bar.

Javascript Calculations

Avatar

Level 1

When I put the following code and run it in a browser it works without issue

var monthstart = '05';
var monthend = '06';
var daystart = '1';
var dayend = '25';
var monthweeks = new Array('4.43','4','4.43','4.3','4.43','4.3','4.43','4.43','4.3','4.43','4.3','4.43');
var monthdays = new Array(31, 28,31,30,31,30,31,31,30,31,30,31);
var numberofdays = 0;
var x = parseFloat(monthend) - 1;
var y = parseFloat(monthstart) - 1;

totalmonths = parseFloat(monthend) - parseFloat(monthstart);
startdays = monthdays[y] - daystart + 1;
enddays = parseFloat(dayend);
alert(totalmonths);
if (totalmonths == 0){
    numberofdays = enddays - daystart + 1;
}

if (totalmonths >0){
    for(var i in monthdays){
        if (i >= y && i < x){
            numberofdays = monthdays[i] + numberofdays;
           
        }
    }
    numberofdays = numberofdays + parseFloat(dayend);
}
if(totalmonths < 0){
   //not coded yet
}
alert("TOTAL DAYS " + numberofdays);

But when I put the same code into my form (Livecycle Designer 8.0) it leaves a blank field.  The startdate and end date fields are date/time types.  For some reason when i just send x to be displayed by the form field it displays a number similar to 100.0074mm instead of 5.

var monthstart = Page1.startdate.rawValue.substr(5,2);
var monthend = Page1.enddate.rawValue.substr(5,2);
var daystart = Page1.startdate.rawValue.substr(8,2);
var dayend = Page1.enddate.rawValue.substr(8,2);

var monthweeks = new Array('4.43','4','4.43','4.3','4.43','4.3','4.43','4.43','4.3','4.43','4.3','4.43');
var monthdays = new Array(31, 28,31,30,31,30,31,31,30,31,30,31);
var numberofdays = 0;
var x = parseFloat(monthend) - 1;
var y = parseFloat(monthstart) - 1;

totalmonths = parseFloat(monthend) - parseFloat(monthstart);
startdays = monthdays[y] - daystart + 1;
enddays = parseFloat(dayend);
alert(totalmonths);
if (totalmonths == 0){
     numberofdays = enddays - daystart + 1;
}

if (totalmonths >0){
     for(var i in monthdays){
         if (i >= y && i < x){
             numberofdays = monthdays[i] + numberofdays;
            
         }
     }
     numberofdays = numberofdays + parseFloat(dayend);
}
if(totalmonths < 0){
    //not coded yet
}

days = numberofdays; //days is name of field on form

Any insight will be greatly appriciated

1 Reply

Avatar

Former Community Member

Two things .....

1 - x and y are reserved words in XFA and are used for the coordinate positions for objects hence you cannot use them as var names. I changed them to x1 and y1 and it worked.

2 - to display a message you would use app.alert with the same parameters not just alert

You can see any javascript errors in the javascript console (ctrl-J) using Acrobat.

Paul