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

FormCalc or Javascript Date Formulas

Mr Spot
Registered: Jan 13 2010
Posts: 8

I have tried to manipulate the numerous information I've found on this topic but can't seem to understand how to get this to work. I'm not much of a programmer and my knowledge of it stops with excel...

I am trying to create a leave request form for our office. I've used the wizard and use the base personal leave template in Adobe Lifecycle Designer and modified as needed.

There are three fields I wish to manipulate StardDate[0] , EndDate[0] & Days[0].

Start Date and End Date are "Date/Time Fields" formatted as such "date{DD-MMM-YY}"

When a user enters in the Start Date and End date using the calendar selections I want the form to automatically calculate the number of working days between these two dates and enter it in the Days[0] Numeric Field.

I tried this http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=3164 This example uses a "calculate" button where is I just want it to do it automatically if possible?

But I still end up with a blank field in Days[0]. I can't seem to understand how to correctly use Date2Num to have it report anything other than 0 nor do I have a clue where to start with Javascript...

Any assistance would be greatly appreciated.

This is the code I've tried so far:

//var dtStart = new Date(StartDate[0])
//var dtEnd = new Date(EndDate[0])
 
var dtStart = new Date(this.getField.StartDate[0].value);
var dtEnd = new Date(this.getField.EndDate[0].value)
 
var nDayStart = dtStart.getDay()
var nDayEnd = dtEnd.getDay()
 
var OneDay = 24 * 60 * 60 * 1000;
 
var TotalNumDays = (dtEnd.getTime() - dtStart.getTime())/OneDay;
// Fix up for Starting on Saturday or sunday
if(nDayStart == 6)
{
   nDayStart = 1;
   TotalNumDays -= 2;
}
else if(nDayStart == 0)
{
   nDayStart = 1;
   TotalNumDays -= 1;
}
// Fix up for ending on saturday or sunday
if(nDayEnd == 6)
{
   nDayEnd = 5;// Make it friday
   TotalNumDays -= 1;
}    
else if(nDayEnd == 0)
{
   nDayEnd = 5;// Make it friday
   TotalNumDays -= 2;
}
 
var NumWeeks = Math.floor(TotalNumDays/7);
 
// Extra is set to 1 to include the end day in the calculation
var extraDays = 1;
var DaysLeft = TotalNumDays - NumWeeks * 7;
if(DaysLeft)
{ // Look for weekend split
   var sepDays = nDayEnd - nDayStart;
   if(sepDays > 0)
      extraDays += sepDays;
   else if(sepDays < 0)// Week end Split
      extraDays += sepDays + 6;
}
var totalWorkingDays = NumWeeks * 5 + extraDays;
this.getField("TotalWorkingDays").value = totalWorkingDays;

Thanks. Chris.

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You are trying to run Acrobat AcroForms JavaScript in LiveCycle Designer. Different language dialects and not completely interchangeable.

George Kaiser

Mr Spot
Registered: Jan 13 2010
Posts: 8
I see. Just to make things more difficult for newbies... Is what I'm trying to do even possible, or will I need a "calculate" button to initiate the calculation?
PetafromOz
Registered: Jun 8 2009
Posts: 230
Hey Mr Spot,

I'm definitely no expert, but have a look at this thread, hopefully it will assist...
http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19450

Good luck,
PetafromOz

from way... underground at Parkes - central West NSW - Australia

Mr Spot
Registered: Jan 13 2010
Posts: 8
Thanks I had a look radzmar's post and tried that code.

if (StartDate[0].rawValue ne null & EndDate[0].rawValue ne null)thenAbs(Date2Num(EndDate[0].formattedValue, DateFmt(2)) - Date2Num(StartDate[0].formattedValue, DateFmt(2)))else$.rawValue = ""endif

But it still reports a value of 0 when the answer isn't 0? I figured if I could get this much working I can then modify to try and get the number of working days. But even this doesn't work for me? What am I doing wrong?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You have to make sure that the format of your date fields match the 'DateFmt(2)' picture.

George Kaiser

Mr Spot
Registered: Jan 13 2010
Posts: 8
Okay, well I tried:

if (StartDate[0].rawValue ne null & EndDate[0].rawValue ne null)thenAbs(Date2Num(EndDate[0].formattedValue, DateFmt(1, "en_AU")) - Date2Num(StartDate[0].formattedValue, DateFmt(1, "en_AU")))else$.rawValue = ""endif

and still get a result of 0. I also reformatted the date time fields to be in this same format. Although I'd prefer them to be in DD-MMM-YY but that doesn't seem to be possible using DateFmt.

Is it not possible to attach things to your posts?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You have to set the various picture formats for the field as you want the date to display. You can type in your own formats for the fields and scripts as long as it complies with the XML format standard. You also have to make sure you are getting the expected display format. I always use the 'formattedValue' for the variable in the FormCalc or JavaScript code.

George Kaiser

Mr Spot
Registered: Jan 13 2010
Posts: 8
Thanks for everyone's help to date.

Unfortunately I'm at a deadend. Nothing has worked and I don't have the time to try and teach myself livecycle javascript from scratch just to generate this one form... Looks like I won't be able to make this part automatic which is disappointing.

Cheers.