首页 > 代码库 > js显示当前时间
js显示当前时间
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="http://www.mamicode.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
//获取当前时间并拼接放到div中的函数
function nowTime(){
var nowDate = new Date();
var year = nowDate.getFullYear();
var month = nowDate.getMonth()+1;
month = month>9 ? month : "0" + month;
var date = nowDate.getDate();
date = date>9 ? date : "0" + date;
var hour = nowDate.getHours();
hour = hour>9 ? hour : "0" + hour;
var miunte = nowDate.getMinutes();
miunte = miunte>9 ? miunte : "0" + miunte;
var second = nowDate.getSeconds();
second = second>9 ? second : "0" + second;
$("#time").text(year+"年"+month+"月"+date+"日"+hour+"时"+miunte+"分"+second+"秒");
}
//加载后执行一次函数,以后每秒再执行
$(function(){
nowTime();
setInterval("nowTime()", 1000);
});
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
js显示当前时间