实时获取当前时间,加个定时器,就可以按需索取,获取时间了!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>获取当前时间</title>
</head>
<body>
<div class="time"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
timeNow();
function timeNow(){
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hour = date.getHours();
var minu =date.getMinutes();
var sec =date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (minu >= 0 && minu <= 9) {
minu = "0" + minu;
}
if (sec >= 0 && sec <= 9) {
sec = "0" + sec;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + hour + seperator2 + minu + seperator2 + sec;
$('.time').html('年月日:'+date.getFullYear() + seperator1 + month + seperator1 + strDate +'时分秒:'+hour + seperator2 + minu +':'+sec );
};
var time = null;
time=setInterval(timeNow,1000);
});
</script>
</body>
</html>
热门评论
楼主好用心,向楼主学习。