JavaScript: Restrict dates on datepicker

1.4k views Asked by At

I am putting together a WordPress site that does this:

  • Allows clients to log in, and
  • Schedule pickup and delivery of their laundry.

Currently, I am using Ninja Forms to take my orders.

Issue
I am trying to modify the datepickers. I have used and modified some WP plugin code to disable past dates, but I can't quite figure out how to do what I want it to do.

I need to disable days in the past and the current day as well.
Here is the code I have thus far:

Plugin:

<?php
  /*
   * Plugin Name: Datepicker Customization - MT Lemon
   */
  add_filter( 'ninja_forms_enqueue_scripts', 'nf_datepicker_options' );
  
  function nf_datepicker_options() {
      wp_enqueue_script( 
        'nf_datepicker_options', 
        plugin_dir_url( __FILE__ ) . 'script.js', 
        array( 'jquery' ), 
        false, 
        true 
      );
  }

script.js:

jQuery( document ).ready( function() {
  new(Marionette.Object.extend( {
    
    initialize: function() {
      this.listenTo(
        Backbone.Radio.channel( 'pikaday' ), 
        'init', 
        this.modifyDatepicker 
      );
    },
    
    modifyDatepicker: function( dateObject, fieldModel ) {
      // Set to future dates ONLY
      dateObject.pikaday.setMinDate( new Date( Date( 'F j, Y' )  ) ) ;   
      // How do I add one day to this value?
      // Block Sundays
      // Block Holidays
    }
  }));
});

I have googled the heck out of this to try and figure out how to show only future days with no avail. I do not have much experience with javascript and everything I find isn't working. Once I figure out how to do this, I want to also remove Sundays from the datepicker as well as remove set days (ie. Holidays).

Is anyone able to help me out, or at least point me in the right direction? Any help would be much appreciated.

Jordan

1

There are 1 answers

2
Ben Rondeau On BEST ANSWER

Using this great answer:

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
dateObject.pikaday.setMinDate(tomorrow);