我有2 calender
我希望calculate
difference
no.of days
我的问题: date
必须在此format dd/mm/yyyy
(日期 - 月 - 年)
这是我的代码段
$( "#datepicker1" ).datepicker();
$( "#datepicker2" ).datepicker();
$("#datepicker2").change(function(){
var date1 = $( "#datepicker1" ).val();
console.log(date1);
console.log("date 2: "+ $(this).val());
var date2 = $(this).val();
//difference calculation
days = (date2- date1) / (1000 * 60 * 60 * 24);
console.log("difference: "+ days);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/mint-choc/jquery-ui.css">
<div class="demo">
<p>Check in Date: <input id="datepicker1" type="text"></p>
</div><!-- End demo -->
<div class="demo">
<p>Check Out Date: <input id="datepicker2" type="text"></p>
</div><!-- End demo -->
答案 0 :(得分:1)
使用moment.js这很容易。类似的东西:
var a = moment('1/1/2012', 'DD/MM/YYYY');
var b = moment('1/1/2013', 'DD/MM/YYYY');
var days = b.diff(a, 'days');
答案 1 :(得分:0)
此代码有助于解决问题的天数
var date1 = $( "#datepicker1" ).val();
var date2 = $(this).val();
date1 = new Date(date1);
date2 = new Date(date2);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);