简体   繁体   中英

How to subtract days from two datepickers?

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM