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

Question

Question

JavaScript for Hiding Weekends and Holidays from Form's Calendar

asked on December 4, 2017

Hey everyone,


I need to know what JavaScript to use in order to hide / gray out a Form's calendar field's holidays and weekends. Has anyone already done this? I'd love to plug and play the code for it if so, thanks!

 

- Ryan 

1 0

Replies

replied on December 6, 2017

Hi Ryan,

Here is a demo for Forms 10.2 and above. You need  to set date field with CSS class 'checkDate' and manually update the "holidays" in script to contain all holidays you want to define.

$(document).ready(function(){
  var holidays = ["2017-12-25", "2018-01-01"];
  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','');
})

 

1 0
replied on December 12, 2017 Show version history

Hi @████████,

 

We have a similar application and use of the script you provided above, except we were looking to restrict it to only Mondays, while restricting holidays.  I was able to make it work with the following (inelegant) change to line 9 of your code:

return (t!=0 && t != 2 && t!=3 && t!=4 && t!=5 && t!=6);

This works, but I'm looking to take it one step further, and have it allow Tuesdays to be chosen if the holiday falls on a Monday.

 

Is this something that is doable with out putting you through the wringer?

 

Thanks

0 0
replied on December 12, 2017

Hi Evan,

You can try update the isCorrectDate function to this:

  function isCorrectDate(n) {
    var date = jQuery.datepicker.formatDate('yy-mm-dd', n);
    var t = n.getDay();
    if (t == 1) {
	return (holidays.indexOf(date) == -1);
    } else if (t == 2) {
    	var yesterday = new Date(n);
	yesterday.setDate(n.getDate() - 1);
    	var yesterdayDate = jQuery.datepicker.formatDate('yy-mm-dd', yesterday);
      	return (holidays.indexOf(yesterdayDate) != -1);
    }
    return false;
  }

 

1 0
replied on December 13, 2017

Thanks @████████!

 

That worked wonderfully! (And is much nicer looking than what I attempted)

 

Thanks!

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

Sign in to reply to this post.