You are viewing limited content. For full access, please sign in.

Question

Question

JavaScript support for excluding weekends.

asked on March 17, 2016 Show version history

Someone can support me.

I created a form of "Vacation Request" where I need the total of days taken (# q10) is calculated, this from an initial date (# q8) and end date (# q9).
Need to be excluded and Saturdays and Sundays not accounted for, I can support with this theme please.

Thank you.

Form Vacation Calc sum tot day without weekends.png
0 0

Replies

replied on March 18, 2016 Show version history

Hi Javier,

This is the script we use on our holiday form in-house. You'll just need to change add a class called "sdate" to you start date field, a class called "edate" to your end date field and a "total" class to your total days field.

function sumtotal() {
        var s = 0;
      	var e = 0;
      	//Get dates from input and reformat, then create date var
        $('.sdate input').each(function () {
          	s1 = $(this).val().replace("-", " ");
          	s2 = s1.replace("-", " 20");
            s = new Date(s2);
        });
      	$('.edate input').each(function () {
          	e1 = $(this).val().replace("-", " ");
          	e2 = e1.replace("-", " 20");
            e = new Date(e2);
        });
      
      var one_day=1000*60*60*24;
      var startMillis = s.getTime();
      var endMillis = e.getTime();
      var totalDays = (endMillis - startMillis)/one_day;
      var weekend = 0;
      var days = 0;
      
      for (var i = startMillis; i < endMillis; i += one_day) {
        var currentDay = new Date(i);
        if (currentDay.getDay() == 5 || currentDay.getDay() == 6) {
          weekend++;
        }
      }
	  	days = (totalDays+1) - weekend;

              
      		//Output value in the total class box
            if (isNaN(days)) {
      			$('.total input').val('Select dates');
            } else {
            	$('.total input').val(days);
            }
      
      	if (e.getTime() < s.getTime()) {
        	$('.edate input').val($('.sdate input').val());
        	sumtotal();
      	}
}

$(document).ready(function () {
  
    $(".sdate input").on('blur change', sumtotal);
  	$(".edate input").on('blur change', sumtotal);

});

Also this only works if the dates are in dd-MMM-yyyy format.

I hope this helps! Dan

1 0
replied on November 4, 2016

hi dan,

 

how can i edit this code to make it work in a table?

i want it to calculate everytime a user adds a row and selects dates as seen below:

 

replied on March 18, 2016

Good Morning.
Probare the script and you discuss your results.
Thank you for your prompt response.  yes

0 0
replied on March 22, 2016 Show version history

Hi Dan.

Integrate JS on the form, but although I used the d= #q8, id= #q9,  id= #q10 

It does not generate the calculation of the total requested vacation days without weekend.

 

I have also used the CSS: class= sdate,  class= edate y class= total

It does not generate the calculation of the total requested vacation days without weekend.

Also I commented that I am using the format DD / MM / YYYY on calendars (d= #q8, id= #q9) or (class= sdate,  class= edate)


Any ideas?

Format Date.png
CSS y JavaScript.png
Format Date.png (62.14 KB)
0 0
replied on April 1, 2016 Show version history

Hi Javier,

The reason it won't be working is because this code requires the date to be in dd-MMM-yy format. Javascript can only parse certain date patterns so to convert from the text input of the date field to a date Javascript can work with it has to be in this format (for this piece of code anyway as I couldn't think of another way to do it)

Cheers, Dan

0 0
replied on July 17, 2016 Show version history

Hi Dan,

Should this work with no change to your script in Forms 10.1?  I need the same functionality and couldn't get the script to work in an existing form, so created a new form with three fields: start date (sdate, dd-MMM-yy), end date (edate, dd-MMM-yy), and total (total, single line).  I then just copied and pasted your script.  In theory that should have worked, however I'm not getting anything at all in the total field.  Is there something additional to this that needs to be done?

Thanks,

Mike

0 0
replied on July 18, 2016

Hi Mike,

My apologies there was an error in my code. I've updated the JS above so that should be correct now. The only other thing you'll need to change is that you'll need to add these classes to the following fields:

Start Date Field: sdate

End Date Field: edate

Total Days Field: total

For example:

I've also updated my post above to include this. Let me know how it goes!

Cheers, Dan

2 0
replied on July 18, 2016

Worked straight away.  Great stuff, thanks Dan.

2 0
You are not allowed to follow up in this post.

Sign in to reply to this post.