Level 1

0% to

Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

## Add days when checkbox is ticked

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile
pizush
Level 1

02-04-2018

Hi

I am using below formcalc script to count weekdays in "Days" field.

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 == 5 | dayOfWeek == 6) then

dayCnt = dayCnt + 1

endif

currentDateNum = currentDateNum + 1

endfor

\$ = TotalDays - dayCnt

else

\$ = ""

endif

else

\$ = ""

endif

I have two check box in the form, chkbox1 and chkbox2.

What I am trying to do, If one person ticked chkbox1 it will add 1(one) day to "Days" field and if one person ticked chkbox2 it will add 2 (two) days to "Days" field.

But i am unable to do it.

#### Accepted Solutions (1)

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile
MinusZero
Level 4

18-04-2018

Thats Great!

Should be able to use some if logic for it. I haven't tried it in javascript but my idea would be this.

Have a first date and last date field. Have the date you want to exclude in another.

In your calculating code, use logic for: if excludedDate is greaterthan the start date AND lessthan the end date, subtract one from the calculation.

An if statement calculating dates looks like this in javascript

if (excludedDate.rawValue > startDate.rawValue | excludedDate.rawValue < endDate.rawValue)

{

//do the calculation

}

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile
pizush
Level 1

18-04-2018

Hi

I removed one endif after \$ = TotalDays - dayCnt and it worked!.

Another point is that if I want to exclude one specific day what type of code I shall use?

Suppose start date is 8th April 2018 and end date is 11th April 2018. I want to exclude 10th April from calculation.

Rgds

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile
MinusZero
Level 4

16-04-2018

What is on line 47 col 4, can you paste a screenshot with the line numbers?

Make sure your if count is correct, an else wont work outside an if statement if it is closed.

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile
pizush
Level 1

16-04-2018

Hi

I changed the code as below after endfor, and showing the error,

Error: Syntax error token near 'else' on line 47, column 4.

if (sickcb1 == 1) then

\$ = TotalDays

else

\$ = TotalDays - dayCnt

endif

endif

else

\$ = ""

endif

else

\$ = ""

endif

Rgds

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile
MinusZero
Level 4

05-04-2018

Are you closing all your if statements? I count 6 started but only 4 ended.

Go to the location in the error and see what is there.

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile

pizush
Level 1

Likes

0 likes

Total Posts

21 posts

0 solutions
View profile
pizush
Level 1

05-04-2018

Hi

Thanks for the response.

After you reply, I put below code in WeekDays

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 == 5 | dayOfWeek == 6) then

dayCnt = dayCnt + 1

endif

currentDateNum = currentDateNum + 1

endfor

if (CB1 == 1) then

\$ = TotalDays – dayCnt + 1

elseif (CB2 == 1) then

\$ = TotalDays – dayCnt + 2

elseif (CB1 && CB2 == 0) then

\$ = TotalDays - dayCnt

endif

else

\$ = ""

endif

else

\$ = ""

endif

But I am getting below error message

As I am newbie in livecycle, please suggest what should I do next to solve the problem?

Thanks

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts

54 solutions
View profile

MinusZero
Level 4

Likes

30 likes

Total Posts

326 posts