js求两个日期差

<script type="text/javascript">
function timeSpan(startDate, endDate) {
var date = "";
if (startDate.length != 0 && endDate.length != 0) {
var startDate = startDate.split("-");
var startDate = new Date(startDate[0], startDate[1]-1, startDate[2]);
var endDate = endDate.split("-");
var endDate = new Date(endDate[0], endDate[1]-1, endDate[2]);
var date = (endDate - startDate) / (1000 * 3600 * 24);
}
document.getElementById('ctl00_ContentPlaceHolder1_lbDayCount').innerText = (date + 1);
}
</script>

起初一开始当跨月时天数总无辜的多出一天,查阅JS API以后才发现JS中月份是从0~11,

所以又原来的var startDate = new Date(startDate[0], startDate[1], startDate[2]);改为var startDate = new Date(startDate[0], startDate[1]-1, startDate[2]);计算一切正常。

你可能感兴趣的:(js)