Solved

limit a second date with jquery datepicker

Posted on 2014-09-16
3
521 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
[X]
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
  • 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

630 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