Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker - calculate date difference

I'm new at this and I really need your help because I've been struggling for days with this… I want to calculate days between two dates chosen with datepicker. Am I on the right track with this or am I doing this completely wrong? All I know is that when I click on Number of days, it doesn't show anything. Here is my code

    <!DOCTYPE html>
    <html>
    <head>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
      <script>
        $(document).ready(function () {

            $("#datepicker").datepicker({ minDate: "01/07/2012", maxDate: "01/30/2012" });

            $("#datepicker1").datepicker({ beforeShow: setminDate });

            var start1 = $('#datepicker');      
            function setminDate() {          
                var p = start1.datepicker('getDate');          
                if (p) { 
                    var k ="01/30/2012";            
                    return {
                    minDate: p,
                    maxDate:k
                }};         
            }           
            function clearEndDate(dateText, inst) {          
                end1.val('');      
            }  
        });
    $(function() {
        $( "#datepicker1" ).datepicker({ dateFormat: 'mm/dd/yyyy' });
        $( "#datepicker" ).datepicker({ dateFormat: 'mm/dd/yyyy' });
    });
    $('button').click(function() {
        var start = $('#datepicker1').datepicker('getDate');
        var end   = $('#datepicker').datepicker('getDate');
        var days   = (end - start)/1000/60/60/24;
        alert(days);
    });
    </script>
    <body>
    <form>
        from: 
        <input id="datepicker" readonly='true'/><br>
        to: 
        <input id="datepicker1" readonly='true'/><br>
    </form>
        <br><button>number of days</button>

    </body> 
    </html>
like image 827
user1154830 Avatar asked Jan 17 '12 20:01

user1154830


1 Answers

working for me after making this change

    var start = $('#datepicker').datepicker('getDate');
    var end   = $('#datepicker1').datepicker('getDate');
    var days   = (end - start)/1000/60/60/24;
    alert(days);

jsfiddle: http://jsfiddle.net/diode/L2et3/3/

like image 51
Diode Avatar answered Oct 21 '22 18:10

Diode