
1. Re: Calculate the number of week days between two dates
Steve L Walker May 10, 2009 9:21 PM (in response to Jo_2005)Try this....
// form1.page1.subform1.WeekEndDays::calculate  (FormCalc, client)
if (HasValue(Start_Date1) & HasValue(End_Date1) ) then
var totalDays = Date2Num(End_Date1, "YYYYMMDD", "en_IE")  Date2Num(Start_Date1, "YYYYMMDD", "en_IE") + 1
var currentDateNum = Date2Num(Start_Date1.formattedValue,"YYYYMMDD")
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
$ = dayCnt
else
""
endifSteve

CalculateWeekDays.pdf 81.4 K


2. Re: Calculate the number of week days between two dates
Jo_2005 May 10, 2009 10:30 PM (in response to Steve L Walker)Dear Steve,
Thank you very much for your reply. I have placed the script into my calculation field called "days1".
There is an error message when the pdf is previewed.
I have attached the error message on a file for you to look at to see where I could be going wrong.
eg. syntax error near token " on line 2, column 0.
Thank you for your assistance.

script failed.pdf 39.4 K


3. Re: Calculate the number of week days between two dates
Steve L Walker May 11, 2009 9:08 AM (in response to Jo_2005)Check...
1) You stated you put the script on the calculate event of "days1". My sample mimics the variable names used in the original post, "Start_Date1" and "End_Date1". If the variable names for the start and end dates are different you will have to change the script to reflect those names.
2) The Date2Num functions in the calculation of "totalDays" uses the date format "YYYYMMDD". If your date patterns differ from "YYYYMMDD" FormCalc will complain.
Steve

4. Re: Calculate the number of week days between two dates
Jo_2005 May 11, 2009 6:58 PM (in response to Steve L Walker)Hello Steve,
I have changed the display and validation pattern in the fields of Start_Date1 and End_Date1 to YYYYMMDD and the scripting works a treat.
Thank you very much for all of your help Steve, it is very much appreciated.

5. Re: Calculate the number of week days between two dates
Jo_2005 May 11, 2009 9:13 PM (in response to Steve L Walker)Hello Steve,
I have one last question for you.
I would like to change the date format from YYYYMMDD to DDMMYYYY.
The Start_Date1 and End_Date1 pattern fields have been changed to the new format of DDMMYYYY.
I have also revised the form calcs in Weekend Days and Total Days to the format of DDMMYYYY.
The script will not calculate the number of days after changing the date format.
Can you please advise what the problem could be?
Thank you once again, Steve.

6. Re: Calculate the number of week days between two dates
Steve L Walker May 11, 2009 9:32 PM (in response to Jo_2005)The attached now displays the start and end dates as DDMMYYYY. I added script to validate the end date is greater than the start date, also.
Steve

CalculateWeekDaysv1dot1.pdf 81.8 K


7. Re: Calculate the number of week days between two dates
Jo_2005 May 11, 2009 10:24 PM (in response to Steve L Walker)Hello Steve,
I have adapted the changes to the script in my form and the number of working days are now calculating between the two dates.
Thank you Steve, you are an absolute legend.

8. Re: Calculate the number of week days between two dates
Jo_2005 May 12, 2009 9:17 PM (in response to Steve L Walker)Hello Steve,
I have revised the script to take into account that if the start date and end date are the same that the total of days will = 1.
Before being revised the script would not calculate the number of days if start and end date were the same.
The revised script is posted as below for reference:
// Weekdays field *This will subtract the total days  the weekend days to give the total number of weekdays between two dates*
if (HasValue(Start_Date1) & HasValue(End_Date1) ) then
$ = TotalDays1  WeekEndDays1
else
""
endif
else
""
endif
// Weekend Field *this will calculate the number of weekend days between two dates*
if
(HasValue(Start_Date1) & HasValue(End_Date1) ) then
if (Date2Num(End_Date1, "YYYYMMDD", "en_IE") >= Date2Num(Start_Date1, "YYYYMMDD", "en_IE")) then
var totalDays = Date2Num(End_Date1, "YYYYMMDD", "en_IE")  Date2Num(Start_Date1, "YYYYMMDD", "en_IE") + 1
var currentDateNum = Date2Num(Start_Date1.formattedValue, "DDMMYYYY")
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
$ =
dayCnt
else
""
endif
else
""
endif
// Total Days Field  *This will calculate the total number of days including weekend days*
if
(HasValue(Start_Date1) & HasValue(End_Date1) ) then
$
= Date2Num(End_Date1, "YYYYMMDD", "en_IE")  Date2Num(Start_Date1, "YYYYMMDD", "en_IE") + 1
else
""
endif
else
""
endif

9. Re: Calculate the number of week days between two dates
shyamkg Dec 26, 2011 10:43 PM (in response to Steve L Walker)Sir,
Belated Merry Christmas...
Just today (27122011) I have come across your solution for calculating the number of weekdays between two dates. It is working perfect.
But I have a small problem. I am residing in Middle East (Dubai  U.A.E). Here the weekends are Friday and Saturday.
If I want to know the number of weekdays between 13Jan2012 and 20Jan2012, your solution will show 6 days, whereas actual is only 5.
How can I modify the script to consider the weekends as Friday and Saturday. Your kind reply would be highly appreciated.
Regards
Shyam Kumar

10. Re: Calculate the number of week days between two dates
Niall O'Donovan Dec 27, 2011 3:39 AM (in response to shyamkg)Hi,
If you have a look at the script, you will see an if statement that is testing the day of the week. Sunday=1 and Saturday=7. You just need to change the 1 to a 6.
if (dayOfWeek == 6  dayOfWeek == 7) then
dayCnt = dayCnt + 1
endif
Good luck,
Niall

11. Re: Calculate the number of week days between two dates
shyamkg Dec 27, 2011 4:38 AM (in response to Niall O'Donovan)Great !! Niall, it worked..Thanks..
Wish you a great year ahead...
Shyam

12. Re: Calculate the number of week days between two dates
Shekh1991 Aug 15, 2014 8:48 PM (in response to Niall O'Donovan)HI Niall/Steve,
Greetings!!
I need a help in calculating no of days which excludes weekend days.
My requirement is that I will have the current date to which I need to add 5 days which excludes weekend days. I need the date to be populated in one of the date fields in the form
for eg: current date is 14/08/2014 and I need the output to be populated as 20/08/2014 Which excludes sat and Sunday.
Could you please let me know how to achieve this.
Many Thanks!

13. Re: Calculate the number of week days between two dates
BR001 Aug 16, 2014 4:23 PM (in response to Shekh1991)Hi,
Try this script, which is JavaScript not FormCalc like those above.
var Days = { Sunday : 0, Monday : 1, Tuesday : 2, Wednesday : 3, Thursday : 4, Friday : 5, Saturday : 6 };
function addWorkDays(date, days, weekend)
{
if (weekend === undefined) weekend = [Days.Saturday, Days.Sunday];
for (var result = date; days > 1; result.setDate(result.getDate()+1))
{
if (weekend.indexOf(result.getDay()) < 0)
{
days;
}
}
return result;
}
console.println(addWorkDays(new Date(2014, 07, 14), 5))
Regards
Bruce

14. Re: Calculate the number of week days between two dates
Shekh1991 Aug 16, 2014 10:08 PM (in response to BR001)Thank you Bruce for the reply. I will try your solution given above.
Just wanted to check if the same can be achieved in process workflow Using a Set value or execute operation.
Any inputs highly appreciated!
Thanks.

15. Re: Calculate the number of week days between two dates
BR001 Aug 18, 2014 4:35 AM (in response to Shekh1991)Hi,
Can help you with process workflow, it's not something I've used, but this is standard JavaScript so would be surprised if it didn't.
Regards
Bruce

16. Re: Calculate the number of week days between two dates
abbasm79416867 Mar 4, 2016 9:33 PM (in response to Jo_2005)Hi, I need a code for calculation like this:
I have three fields like this:
"Date field", "Numbers of Day"s and "Calculate Date fields"
I like to calculate like this: Date field + Numbers of Days = Calculate Date (Excluding Weekend days)
I need javascript for this calculation.