Bootstrap Datepicker

http://stackoverflow.com/q/24981072/1093087

by Nigam Shah

HTML

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<div id="sandbox-container">
    <input type="text" type="text" class="form-control" />
</div>

<div>
<button id="showdate">Show selected Date</button>
</div>

JavaScript

$('#sandbox-container input').datepicker({
    autoclose: true
});

$('#sandbox-container input').on('show', function(e){
    console.debug('show', e.date, $(this).data('stickyDate'));
    
    if ( e.date ) {
         $(this).data('stickyDate', e.date);
    }
    else {
         $(this).data('stickyDate', null);
    }
});

$('#sandbox-container input').on('hide', function(e){
    console.debug('hide', e.date, $(this).data('stickyDate'));
    var stickyDate = $(this).data('stickyDate');
    
    if ( !e.date && stickyDate ) {
        console.debug('restore stickyDate', stickyDate);
        $(this).datepicker('setDate', stickyDate);
        $(this).data('stickyDate', null);
    }
});

$('#showdate').on("click",function() {
	var selecteddate = $("#sandbox-container input").data('datepicker').getFormattedDate('yyyy-mm-dd');

	if(selecteddate == "")
  {
  	alert("no date selected");
  }else
  {
  	alert(selecteddate);
  }
});