<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function bodyLoad(){
var dateTime=new Date();
var hh=dateTime.getHours();
var mm=dateTime.getMinutes();
var ss=dateTime.getSeconds();
var yy=dateTime.getFullYear();
var MM=dateTime.getMonth()+1; //1月返回为0,所以加1
var dd=dateTime.getDate();
var week=dateTime.getDay();
var days=[ "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 ",]
document.getElementById("date").value=yy+"年"+MM+"月"+dd+"日 "+ hh+"时"+mm+"分"+ss+"秒 星期"+days[week] ;
document.getElementById("time").value=yy+"-"+MM+"-"+dd+" "+hh+":"+mm+":"+ss;
setTimeout(bodyLoad,1000);
}
function setCurTime(){
var now=new Date();
var year=now.getYear()+1900;//这个方法拿到的是1900年距离现在的年份
var month=now.getMonth()+1;
var day=now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
var timeString = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
var oCtl = document.getElementById('test');
oCtl.value = timeString;
setTimeout(setCurTime,1000);
}
用途:将js字符串对象转换为js中的date对象
输入:
str:字符串日期 "2009-12-02 12:25";
返回:
js date日期对象
*/
var tempStrs = str.split(" ");
var dateStrs = tempStrs[0].split("-");
var year = parseInt(dateStrs[0], 10);
var month = parseInt(dateStrs[1], 10) - 1;
var day = parseInt(dateStrs[2], 10);
var timeStrs = tempStrs[1].split("-");
var hour = parseInt(timeStrs [0], 10);
var minute = parseInt(timeStrs[1], 10) - 1;
var second = parseInt(timeStrs[2], 10);
var date = new Date(year, month, day, hour, minute, second);
return date;
}
</script>
</head>
<body onload="bodyLoad(),setCurTime()">
<input type="text" id="date" size = "100"/>
<br/>
<input type="text" id="time" size = "100" />
<br/>
<input type="text" id="test" size = "100" />
</body>
</html>