Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Add days when checkbox is ticked

Avatar

Level 2

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.

Can anyone please help me how to solve this as I am new to livecycle forms?

1 Accepted Solution

Avatar

Correct answer by
Level 7

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

}

View solution in original post

7 Replies

Avatar

Level 7

Hi,

After you have endfor, can you just add some if statements to cover if chkbox1 is checked, chkbox2 is checked or neither is checked and make the calculations?

Avatar

Level 2

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

screen-13.23.50[05.04.2018].png

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

Thanks

Avatar

Level 7

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.

Avatar

Level 2

Hi

Thanks for the reply

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

can you please help

Rgds

Avatar

Level 7

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.

Avatar

Level 2

Hi

Thank for reply.

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

Avatar

Correct answer by
Level 7

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

}