javascript時(shí)間函數(shù)
Date對(duì)象有多種構(gòu)造函數(shù):
new Date() //當(dāng)前時(shí)間javascript顯示時(shí)間實(shí)例
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// 當(dāng)數(shù)字是小于10的就要在前面加0.看起來規(guī)范
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',1000)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
JavaScript顯示系統(tǒng)時(shí)間帶日期
JavaScript顯示系統(tǒng)時(shí)間,顯示年月日時(shí)分秒和星期,每秒自動(dòng)刷新,并加入日期顏色區(qū)別:
星期一至星期五顯示為黑色
星期六顯示為綠色
星期日顯示為紅色
<!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>JavaScript 時(shí)間顯示</title>
</head>
<body>
<span id=localtime></span>
<script type="text/javascript">
function showLocale(objD)
{
var str,colorhead,colorfoot;
var yy = objD.getYear();
if(yy<1900) yy = yy+1900;
var MM = objD.getMonth()+1;
if(MM<10) MM = '0' + MM;
var dd = objD.getDate();
if(dd<10) dd = '0' + dd;
var hh = objD.getHours();
if(hh<10) hh = '0' + hh;
var mm = objD.getMinutes();
if(mm<10) mm = '0' + mm;
var ss = objD.getSeconds();
if(ss<10) ss = '0' + ss;
var ww = objD.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="星期日";
if (ww==1) ww="星期一";
if (ww==2) ww="星期二";
if (ww==3) ww="星期三";
if (ww==4) ww="星期四";
if (ww==5) ww="星期五";
if (ww==6) ww="星期六";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;
return(str);
}
function tick()
{
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
window.setTimeout("tick()", 1000);
}
tick();
</script>
</body>
</html>
javascript實(shí)時(shí)顯示當(dāng)天日期的方法
代碼如下:
<!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>js顯示時(shí)間</title>
</head>
<body>
<div id="divBottom"></div>
<script type="text/javascript">
//獲取當(dāng)天日期
function date()
{
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var hour="00"+date.getHours();
hour=hour.substr(hour.length-2);
var minute="00"+date.getMinutes();
minute=minute.substr(minute.length-2);
var second="00"+date.getSeconds();
second=second.substr(second.length-2);
var week=date.getDay();
switch(week)
{
case 1:week="星期一 ";break;
case 2:week="星期二 ";break;
case 3:week="星期三 ";break;
case 4:week="星期四 ";break;
case 5:week="星期五 ";break;
case 6:week="星期六 ";break;
case 0:week="星期日 ";break;
default:week="";break;
}
document.getElementById("divBottom").innerHTML=year+"年"+month+"月"+day+"日"+" "+hour+":"+minute+":"+second+" "+week;
}
setInterval("date()",1000);
</script>
</body>
</html>
運(yùn)行效果如下圖所示:
js顯示當(dāng)前系統(tǒng)時(shí)間的代碼
js獲取當(dāng)前系統(tǒng)時(shí)間
js顯示當(dāng)前系統(tǒng)時(shí)間的代碼
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>系統(tǒng)時(shí)間</title>
<script language="javascript" type="text/javascript">
<!--
//獲得當(dāng)前時(shí)間,刻度為一千分一秒
var initializationTime=(new Date()).getTime();
function showLeftTime()
{
var now=new Date();
var year=now.getYear();
var month=now.getMonth();
var day=now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
document.all.show.innerHTML=""+year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds+"";
//一秒刷新一次顯示時(shí)間
var timeID=setTimeout(showLeftTime,1000);
}
//-->
</script>
</head>
<body onload="showLeftTime()">
<label id="show">顯示時(shí)間的位置</label>
</body>
</html>
更多建議: