Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Total Days between two Dates Excluding Weekends in LCD

Avatar

Level 1

Want to get total days from two dates

Date1

Date2

the total days should exclude weekends "Fridays" and "Saturdays"

I'm pushing the script in properties of the Total field.

could anybody check if I'm doing something wrong because the calculation is not showing the correct number.

the code is given below:

// get the end date value

var Date2 = this.getField("Date1").value;

// get the start date value

var Date1 = this.getField("Date2").value;

var dDate1 = util.scand("dd/mmm/yyyy", Date1);

var dDate2 =util.scand("dd/mmm/yyyy", Date2);

event.value = calcBusinessDays(dDate2, dDate1)-1;

if(event.value == 0) event.value = "";

function calcBusinessDays(dDate2, dDate1) { // input given as Date objects

    var iWeeks, iDateDiff, iAdjust = 0;

    if (dDate1 < dDate2) return -1; // error code if dates transposed

    var iWeekday1 = dDate2.getDay(); // day of week

    var iWeekday2 = dDate1.getDay();

    iWeekday1 = (iWeekday1 == 5) ? 7 : iWeekday1; // change Sunday from 0 to 7

    iWeekday2 = (iWeekday2 == 6) ? 7 : iWeekday2;

    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend

    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays

    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)

    iWeeks = Math.floor((dDate1.getTime() - dDate2.getTime()) / 604800000)

     if (iWeekday1 <= iWeekday2) {

      iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)

    } else {

      iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)

    }

    iDateDiff -= iAdjust // take into account both days on weekend

return (iDateDiff + 1); // add 1 because dates are inclusive

    if (Date1=="" || Date2=="") event.value = "";

else {

  // do the rest of the calculation

}

  }

i want to use only one text field to calculate working days excluding weekends (Fridays and Saturdays)

the above code is working fine with Acrobat JS in the field properties, but not working here.

1 Accepted Solution

Avatar

Correct answer by
Level 2

You can use below code

var fromDate = Date1

var toDate = Date2

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

                                        dayCnt = dayCnt + 1

                              endif

                              currentDateNum = currentDateNum + 1

endfor

$ = TotalDays - dayCnt

          else

$ = ""

          endif

else

          $ = ""

endif

View solution in original post

8 Replies

Avatar

Correct answer by
Level 2

You can use below code

var fromDate = Date1

var toDate = Date2

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

                                        dayCnt = dayCnt + 1

                              endif

                              currentDateNum = currentDateNum + 1

endfor

$ = TotalDays - dayCnt

          else

$ = ""

          endif

else

          $ = ""

endif

Avatar

Level 1

jahntech, many thanks for prompt replies..

now it is showing 6 working days if a person leaves on 1 apr 2018 and back to work on (Date2) 8 apr 2018.

i want it to show 5 days and two weekend days (Fri and Sat) .... keep in mind that Date2 is joining date which means that the total days should subtract one day from the joining date.

could you please help?

Avatar

Level 2

I am just like an user like u. I don't have any experience in LCD form. By preparing my acroform I just got this code from adobe forum, so I shared it with some adjustment. If u give some time with the code, I hope u could solve.

Working with date calculation is not easy for users or who have not coding knowledge. The more you search the more u learn and it will help u in futures.

Avatar

Level 1

dear jahantech,

the follwoing code worked like a charm!.... i just added "-1" in the end of the following line:

var toDateAsNum =Date2Num(toDate, "YYYY-MM-DD", "en_IE")-1.

Avatar

Level 1

now im very sorry to add one more question here but its urgent please

i also want to exclude the public holidays (solar and lunar) how could i do it?

Avatar

Level 2

You have to define holidays first then u have to compare those with total days. If it falls within then u have to deduct one from calculation.

Since lunar holiday is change prone for that u can use a different field for it and for solar holidays u can keep in background.

Just an idea I am giving to u.

Avatar

Level 1

jahantech your prompt replies are really highly appreciated. very sorry to say but could you or anybody please help me with providing an example of this complicated code.

Avatar

Level 2

For acroform u can use below

Calculation problem: Need script modification

From that thread u will find holiday array. You can change the holiday array in formcalc and use it in your code