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

Question

Question

Subtracting Holidays from Time Off Requests

asked on January 9, 2024

This is a tricky situation.

 

I am trying to subtract Holidays that I manually put into the shown Javascript in order to calculate the days a person is trying to request off.  I was able to get the script to remove weekends as well as half days from the form, but holidays are a whole other issue.

 

Attached is the script, along with an image of the form.

 

$(document).ready(function(){
  var holidays = ["2024-05-28", "2024-11-28", "2024-12-24", "2024-12-25", "2024-12-31"];
  function isCorrectDate(n) {
    var date = jQuery.datepicker.formatDate('yy-mm-dd', n);
    if (holidays.indexOf(date) != -1) {
        return false;
      } else {
        var t = n.getDay();
        return (t!=6 && t != 0);
      }
  }
  function checkDate(n) {
    return[isCorrectDate(n),""];
  }
  function checkField(input, inst) {
    if ($(input).closest('li').hasClass('checkDate')) {
      $(input).datepicker("option", {beforeShowDay: checkDate});
    }
  }
  $.datepicker.setDefaults( {beforeShow: checkField} );
  window.Parsley.addValidator('noweekend', {
    validateString: function(value) {
    console.log(value);
      return isCorrectDate(new Date(value));
    },
    messages: {
      en: 'Not valid date.'
    }
  });
  $('.checkDate input').attr('data-parsley-noweekend','');
})

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 = Math.round((endMillis - startMillis)/one_day);
      var weekend = 0;
      var days = 0;
      var holidays = 0;
      
      for (var i = startMillis; i < endMillis; i += one_day) {
        var currentDay = new Date(i);
        if (currentDay.getDay() == 5 || currentDay.getDay() == 6) {
          weekend++;
        }
      }
  
  	  //Check whether the half day radio button is "Yes"
  	  var choice = $('.halfday :checked').val();
        if (choice == 'Yes') {
          days = (totalDays+1) - 0.5 - weekend - holidays;
        } else {
		  days = (totalDays+1) - weekend - holidays;
        }
          
      		//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);
  	$(".halfday input").on('blur change', sumtotal);  
    $(".read-only *").prop("readonly", true);

});

 

Form Image.png
Form Image.png (17.33 KB)
0 0

Answer

SELECTED ANSWER
replied on January 9, 2024 Show version history

You can use JavaScript, but you might be able to do this with out-of-the-box formulas/calculations, which would also make it compatible with the modern designer.

For example, NETWORKDAYS() allows you to calculate days and pass in an array of holidays, which will be excluded from the count; you can either use the default weekends or pass in a fourth parameter to define custom weekends.

NETWORKDAYS(startDateendDate, [holidays])

You can define your holidays as an array of DATE formulas/values like so

[DATE(2024,05,28),DATE(2024,11,28)]

Then plug that into the formula along with your start/end field variables

From there you can expand on the formula with an IF or something like that for Half Days

IF(Half_Day="Yes",true,false)

The result would be something like this if I'm understanding your half day logic correctly (just have to match up your variable names and complete the holiday list).

=NETWORKDAYS(Starting_Date, Ending_Date, [DATE(2024,05,28),DATE(2024,11,28)])-IF(Half_Day="Yes",0.5,0)

Formulas in Laserfiche Forms

 

Another advantage of formulas is that they work the same if the field is read only so you don't have to do anything special like you do when you set values with JavaScript.

2 0
replied on January 9, 2024

Jason,

 

Thank you so much.  This was exactly what I needed!  I tried using NETWORKDAYS previously and couldn't get it to work. 

 

Thank you!

0 0

Replies

replied on November 7, 2024

Hi, we have supported configuring holiday calendars on Forms 12.

You can see other changes from: Laserfiche 12 Changelog

Get Laserfiche Forms 12 package from: Laserfiche 12 - Downloads

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

Sign in to reply to this post.