Hi,
I have an annual form which i would like for it to calculate the working days between two dates. I have an error when i select the start date to be e.g. Thursday 31 October and end date Monday 4th Nov it only seems to caluculate 1 weekend day? I am using formcalc
I also have another issue as my table has buttons that allows for more lines to be added if more that one set of dates is to be added, how to i get the table to calculate the different date working days and then calculate the total days altogether?
Any help would be great
Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
There's a problem with the calculate script where you're calculating the weekend days.
The problematic line is this one:
var currentDateNum = Date2Num(xfa.resolveNode("#subform.#subformSet.#subform[1].#subform[2].Table_SF.Table1.row1.Start_Date1").formattedValue,"DD-MM-YYYY")
If you print out the value, you'll see that it is always set to zero.
Change it to the following to fix the issue:
var currentDateNum = Date2Num(xfa.resolveNode("#subform.#subformSet.#subform[1].#subform[2].Table_SF.Table1.row1.Start_Date1"), "YYYY-MM-DD", "en_IE")
Views
Replies
Total Likes
Question 1: this is javascript on mouseUp:
var dtStart = new Date(this.getField("StartDate").value);
var dtEnd = new Date(this.getField("EndDate").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);
var extraDays = 1;
var DaysLeft = TotalNumDays - NumWeeks * 7;
if(DaysLeft)
{
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;
I have a sample I can email if interested.
Views
Replies
Total Likes
Hi Paul,
yes please if you could send a sample through that would help.
Where do i place this formula, in the total days box? Will it still work as i normally use forcalc?
Mnay Thanks
Rhian
Views
Replies
Total Likes
That's Acrobat JavaScript so it will need to be edited to work for LiveCycle.
Views
Replies
Total Likes
Is anyone able to have a look at my form please?
I am still stuggling on the calculation for all the days to total up taken and plus it always shows the number that is in the first collumn of the table and not the number between the two dates.
Thanks
Views
Replies
Total Likes
Post your form somewhere and it will be much easier to help you. It's extremely difficult to debug a problem with only an explanation of what is occurring.
Views
Replies
Total Likes
Hi Justin,
thanks, ive placed the form here https://www.dropbox.com/sh/ji4vwxkq6ixm2do/CFoDWILemm
Many thanks
Views
Replies
Total Likes
There's a problem with the calculate script where you're calculating the weekend days.
The problematic line is this one:
var currentDateNum = Date2Num(xfa.resolveNode("#subform.#subformSet.#subform[1].#subform[2].Table_SF.Table1.row1.Start_Date1").formattedValue,"DD-MM-YYYY")
If you print out the value, you'll see that it is always set to zero.
Change it to the following to fix the issue:
var currentDateNum = Date2Num(xfa.resolveNode("#subform.#subformSet.#subform[1].#subform[2].Table_SF.Table1.row1.Start_Date1"), "YYYY-MM-DD", "en_IE")
Views
Replies
Total Likes
Thanks Justin it works! Now for the other problem how can i get it to claculate each new line and total the working days?
Views
Replies
Total Likes
Add this to the calculate event for your workdays field (I also cleaned up the code a bit):
var fromDate = Start_Date1
var toDate = End_Date1
if ( HasValue(fromDate) & HasValue(toDate) ) then
var fromDateAsNum = Date2Num(fromDate, "YYYY-MM-DD", "en_IE")
var toDateAsNum = Date2Num(toDate, "YYYY-MM-DD", "en_IE")
if (toDateAsNum > fromDateAsNum) then
var TotalDays = toDateAsNum - fromDateAsNum + 1
var currentDateNum = fromDateAsNum
var dayCnt = 0
for i=1 upTo TotalDays do
var dayOfWeek = Num2Date(currentDateNum,"E")
if (dayOfWeek == 1 | dayOfWeek == 7) then
dayCnt = dayCnt + 1
endif
currentDateNum = currentDateNum + 1
endfor
$ = TotalDays - dayCnt
else
$ = ""
endif
else
$ = ""
endif
Thank you Justin!!!! I have been trying to sort this out for ages and didnt think it was possible!
Only one small thing now, what is the calculation for adding them all together in the totalwork days?
Amzing!!! Thank you!
Views
Replies
Total Likes
its ok i have done it Thanks for all the help!
Views
Replies
Total Likes
is there any way that if you only enter the strat date and the end date to be the same date it will give you 1 day as currently it doesnt show anything?
Thanks
Views
Replies
Total Likes
To add them all together, put this in your calculate script for the total field:
$ = Sum( row1[*].workdays )
Views
Replies
Total Likes
The reason start and end date shows nothing is because of this line:
if (toDateAsNum > fromDateAsNum) then
The calculation only runs if end date is greater than begin date. Change the expression to >= to get the desired result.
Thank you Justin My form is complete! (hopefully)
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies