自己根据需要改改额<!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=gb2312" />
<title>javscript</title>
<script type="text/javascript">
function getvalue()
{
var one=document.getElementById("date1").value;
var two=document.getElementById("date2").value;
var parr=/[-:]/;
while(parr.exec(one))
{
one=one.replace(parr,' ');
}
while(parr.exec(two))
{
two=two.replace(parr,' ');
}
var array1=one.split(" ");
var date1=new Date(array1[0],array1[1],array1[2],array1[3],array1[4]);
var array2=two.split(" ");
var date2=new Date(array2[0],array2[1],array2[2],array2[3],array2[4]);
var days=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(24*60*60*1000));
var hours=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*60*1000)%24);
var minitus=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*1000)%60);
document.getElementById("days").value=days+"天"+hours+"小时"+minitus+"分";
}
</script>
</head>
<body>
日期一:<input id="date1"type="text" size="20" />*:日期的格式:1900-01-04 08:00<br />
日期二:<input id="date2"type="text" size="20" onblur="getvalue()" /><br />
相 差:<input id="days" type="text" size="20" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javscript</title>
<script type="text/javascript">
function getvalue()
{
var one=document.getElementById("date1").value;
var two=document.getElementById("date2").value;
var parr=/[-:]/;
while(parr.exec(one))
{
one=one.replace(parr,' ');
}
while(parr.exec(two))
{
two=two.replace(parr,' ');
}
var array1=one.split(" ");
var date1=new Date(array1[0],array1[1],array1[2],array1[3],array1[4]);
var array2=two.split(" ");
var date2=new Date(array2[0],array2[1],array2[2],array2[3],array2[4]);
var days=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(24*60*60*1000));
var hours=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*60*1000)%24);
var minitus=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*1000)%60);
document.getElementById("days").value=days+"天"+hours+"小时"+minitus+"分";
}
</script>
</head>
<body>
日期一:<input id="date1"type="text" size="20" />*:日期的格式:1900-01-04 08:00<br />
日期二:<input id="date2"type="text" size="20" onblur="getvalue()" /><br />
相 差:<input id="days" type="text" size="20" />
</body>
</html>
2.<input type="text" name="3" readOnly onClick="setDayHM(this);">以上是我实际用到的代码注:1 和 2 文本框,都是通过JS控件选择日期时间的,得出格式为:2009-03-17 08:00 日期一:<input id="date1"type="text" size="20" />*:日期的格式:1900-01-04 08:00<br />
日期二:<input id="date2"type="text" size="20" onblur="getvalue()" /><br />
相 差:<input id="days" type="text" size="20" />
独立运行"lxy0502030314 ”给的JS程序 是可以的,但是无法与我相结合,该如何实现和你的程序结合一起?本人对JS不熟悉, 望高人指点迷津~~
//为何:改为:var one=document.getElementByName ,就不能运行? 谁帮我看看<!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=gb2312" />
<title>javscript</title>
<script type="text/javascript">
function getvalue()
{
var one=document.getElementByName("date1").value;
var two=document.getElementByName("date2").value;
var parr=/[-:]/;
while(parr.exec(one))
{
one=one.replace(parr,' ');
}
while(parr.exec(two))
{
two=two.replace(parr,' ');
}
var array1=one.split(" ");
var date1=new Date(array1[0],array1[1],array1[2],array1[3],array1[4]);
var array2=two.split(" ");
var date2=new Date(array2[0],array2[1],array2[2],array2[3],array2[4]);
var days=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(24*60*60*1000));
var hours=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*60*1000)%24);
var minitus=Math.floor((parseFloat(date2.getTime())-parseFloat(date1.getTime()))/(60*1000)%60);
document.getElementByName("days").value=days+"天"+hours+"小时"+minitus+"分";
}
</script>
</head>
<body>
日期一:<input name="date1"type="text" size="20" />*:日期的格式:1900-01-04 08:00<br />
日期二:<input name="date2"type="text" size="20" onblur="getvalue()" /><br />
相 差:<input name="days" type="text" size="20" />
</body>
</html>
getElementByName 用于传入后台,name可以相同