Bạn có thể dùng hiệu ứng JavaScript này để tìm số lượng ngày giữa hai thời điểm nào đó.
JavaScript:
<script type="text/javascript">
// Created by: JavaScript Demos :: http://www.javascript-demos.com/
function calcDays(){
var date1 = document.getElementById('d1').lastChild.data;
var date2 = document.getElementById('d2').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diffDays').innerText = daysApart;
}
onload=calcDays;
</script>
HTML:
<table width='220' border='1' cellspacing='0' cellpadding='5' align="center">
<tr>
<td>Starting<br>Date</td>
<td>Ending<br>Date</td>
<td>Total<br>Days</td>
</tr><tr>
<td id='d1'>01-01-2006</td>
<td id='d2'>08-05-2006</td>
<td id='diffDays' align='center'></td>
</tr>
</table>
JavaScript:
<script type="text/javascript">
// Created by: JavaScript Demos :: http://www.javascript-demos.com/
function calcDays(){
var date1 = document.getElementById('d1').lastChild.data;
var date2 = document.getElementById('d2').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diffDays').innerText = daysApart;
}
onload=calcDays;
</script>
HTML:
<table width='220' border='1' cellspacing='0' cellpadding='5' align="center">
<tr>
<td>Starting<br>Date</td>
<td>Ending<br>Date</td>
<td>Total<br>Days</td>
</tr><tr>
<td id='d1'>01-01-2006</td>
<td id='d2'>08-05-2006</td>
<td id='diffDays' align='center'></td>
</tr>
</table>
0 comments:
Post a Comment