Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

limit a second date with jquery datepicker

Posted on 2014-09-16
3
500 Views
Last Modified: 2014-09-16
I am using jquery datepicker for a "from" and "to" date.
I am trying to limit the "to" date to 7 business days (9 actual days) after the from date.
User should be able to pick any date after 2days in the future and then the "to" date would be any date from that date up to 9 days out.

I tried this and it works as long as the from date is the first available date, but fails after that.
  $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+2d",
      minDate: "+2d",
      changeMonth: true,
      beforeShowDay: $.datepicker.noWeekends,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
   
    $( "#to" ).datepicker({
          defaultDate: "+2d",
      maxDate: "1w +4d",
      changeMonth: true,
      beforeShowDay: $.datepicker.noWeekends,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });

I don't know how to get the "from" date and add "1w +4d" to it.

Thanks for any help
james
0
Comment
Question by:James Stone
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40327074
Test page : http://jsfiddle.net/ycsak53c/1/

 $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+2d",
      changeMonth: true,
      beforeShowDay: $.datepicker.noWeekends,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
        var maxDate = new Date(new Date(selectedDate).getTime() + (4 * 7 + 2) * 24 * 3600 * 1000);
        $( "#to" ).datepicker( "option", "maxDate", maxDate );
      }
    });
    
    $( "#to" ).datepicker({ 
          defaultDate: "+2d",
      changeMonth: true,
      beforeShowDay: $.datepicker.noWeekends,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });

Open in new window

0
 

Author Comment

by:James Stone
ID: 40327161
This works!  you have 30 days ahead, but I figured it out.

Thanks for the quick reply!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40327173
(4 * 7 + 2) -> (1 * 7 + 4)

my bad...
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question