javascrip计算两个时间差
<script language="JavaScript">
//计算天数差的函数,通用
function DateDiff(starttime,endtime,diff)
{
//sDate1和sDate2是2002-12-18格式
var sDate1=starttime.value;
var sDate2=endtime.value;
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
//转换为12-18-2002格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
// alert(oDate1 - oDate2)
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24)
//把相差的毫秒数转换为天数
diff.value=iDays;
}
//得当前时间的方法
function getDates(falgs)
{
var today = new Date();
var todayStr = today.getYear() + "-";
if(today.getMonth()<10){
todayStr = todayStr +"0" +(today.getMonth()+1)+"-";
}
else{
todayStr = todayStr+(today.getMonth()+1)+"-";
}
if(today.getDate()<10){
//alert("11"+today.getDate());
todayStr = todayStr + "0" +today.getDate();
}
else{
todayStr = todayStr + today.getDate();
}
todayStr = todayStr + "";
return todayStr;
}
</script>
<html>
<body>
<form name="form1">
开始时间<input type="text" name="createtime">
结束时间<input type="text" name="endtime">
时间差<input type="text" name="diff">
<input type="button" value="确定" onclick="DateDiff(document.all.createtime,document.all.endtime,document.all.diff)">
</form>
</body>
</html>