jQuery UI: Date Picker

Date Picker

by sgearhart2

HTML

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/mint-choc/jquery-ui.css">
<div class="demo">

<p>End date: <input id="datepicker_end" type="text"></p>
 Start date: <input id="datepicker_start" type="text" >

</div><!-- End demo -->

<div style="display: none;" class="demo-description">
<p>The datepicker is tied to a standard form input field.  Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay.  Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.</p>
</div><!-- End demo-description -->

CSS

/*
    Add custom styling or use existing jQuery UI themes

    REFERENCE: jQuery UI themes, http://blog.jqueryui.com/
*/

JavaScript

var dates = [];
$( "#datepicker_end" ).datepicker({
      numberOfMonths: 3,
      changeMonth: true,
      changeYear : true,
      beforeShow: function() {
        console.log('show');
      	dates = [];
      },
      onSelect: function(dateText, inst) {
        console.log('select');
      	dates.push(new Date(dateText));
        if(dates.length < 2){
    			$(this).data('datepicker').inline = true;  
        }
        else {
        	dates.sort(function(a,b){return a > b;});
    			$(this).data('datepicker').inline = false; 
          console.log((dates[1].getMonth()+1) + '/' + dates[1].getDate() + '/' + dates[1].getFullYear());
        	$(this).datepicker( "setDate", (dates[1].getMonth()+1) + '/' + dates[1].getDate() + '/' + dates[1].getFullYear() ); 
        	$('#datepicker_start').val((dates[0].getMonth()+1) + '/' + dates[0].getDate() + '/' + dates[0].getFullYear());
          $(this).blur();
        }
        console.log(dates);

      }
 });