0

I have two datepickers, namely check-in and check-out dates. I want to get the difference of those dates and output it in a JavaScript variable, so I can get it to a PHP variable. How do I do it?

For example:

Check -in : 01 / 15 / 2017
Check - out: 01 / 18 / 2017

I shall get 3 days.

Code I have so far: jsFiddle

Ken Flake
  • 585
  • 8
  • 28
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Sean Jan 15 '17 at 04:45
  • I didn't get it. Can you explain better? If you add a form tag and a submit button you will be able to get the values in the yourfile.php: https://jsfiddle.net/tuL7dmcm/ – wcomnisky Jan 15 '17 at 04:51

1 Answers1

2

Try this...

HTML

<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>

JS:

$("#firstDate").datepicker({ }); 
$("#secondDate").datepicker({
    onSelect: function () {
        myfunc();
    }
}); 

function myfunc(){
    var start= $("#firstDate").datepicker("getDate");
    var end= $("#secondDate").datepicker("getDate");
    days = (end - start) / (1000 * 60 * 60 * 24);
    alert(Math.round(days));
}

Fiddle :https://jsfiddle.net/tbwa1m8c/26/

Dexter Bengil
  • 5,995
  • 6
  • 35
  • 54
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19