您的位置:首页 > 其它

计算两个日期相差的天数

2009-07-03 11:09 260 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
function strToDate(str)
{
var arys= new Array();
arys=str.split('-');
var newDate=new Date(arys[0],parseInt(arys[1], 10) - 1,arys[2]);
return newDate;
}
function days_between(date1,date2){
var ONE_DAY=1000*60*60*24;
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();

var difference_ms = Math.abs(date1_ms- date2_ms);
return Math.round(difference_ms/ONE_DAY);
}
function plus(){
var date1 = strToDate(document.forms[0].date1.value);
var date2 = strToDate(document.forms[0].date2.value);
alert("日期相差天数为:" + days_between(date1,date2));
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>Date1
<input type="text" name="date1" />
</label>
<p>
<label>Date2
<input type="text" name="date2" />
</label>
</p>
<p>
<label>Compute
<input type="submit" name="Submit" value="提交" onclick="plus()" />
</label>
</p>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: