JS 顯示當(dāng)前日期與時(shí)間的代碼
<script language="Javascript">
var datelocalweek=new Array("星期日", "星期一", "星期二","星期三","星期四", "星期五","星期六");
var datelocalnow=new Date();
var datelocalyear=datelocalnow.getFullYear();
var datelocalmonth=(datelocalmonth="0"+(datelocalnow.getMonth()+1)).substr(datelocalmonth.length-2,2);
var datelocalday=(datelocalday="0"+datelocalnow.getDate()).substr(datelocalday.length-2,2);
var datelocalweekday=datelocalweek[datelocalnow.getDay()];
document.write(datelocalyear+"年"+datelocalmonth+"月"+datelocalday+"日"+" "+datelocalweekday);
</script>
效果如下圖:

相應(yīng)的JS如下:
//創(chuàng)建一個(gè)數(shù)組,用于存放每個(gè)月的天數(shù)
function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) {
this[0] = m0;
this[1] = m1;
this[2] = m2;
this[3] = m3;
this[4] = m4;
this[5] = m5;
this[6] = m6;
this[7] = m7;
this[8] = m8;
this[9] = m9;
this[10] = m10;
this[11] = m11;
}
//實(shí)現(xiàn)月歷
function calendar() {
var monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec";
var today = new Date();
var thisDay;
var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
year = today.getYear() +1900;
thisDay = today.getDate();
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
monthDays[1] = 29;
nDays = monthDays[today.getMonth()];
firstDay = today;
firstDay.setDate(1);
testMe = firstDay.getDate();
if (testMe == 2)
firstDay.setDate(0);
startDay = firstDay.getDay();
var dayNames = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var monthNames = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
var now = new Date();
document.write("<div id='rili' style='position:absolute;width:140px;left:200px;top:50px;'>")
document.write("<TABLE width='217' BORDER='0' CELLSPACING='0' CELLPADDING='2' BGCOLOR='#0080FF'>")
document.write("<TR>");
document.write(" <TD> ");
document.write(" <table border='0' cellspacing='1' cellpadding='2' bgcolor='Silver'>");
document.write(" <TR><th colspan='7' bgcolor='#C8E3FF'>");
document.writeln(" <FONT STYLE='font-size:9pt;Color:#330099'>" + "公元 " + now.getYear() + "年" + monthNames[now.getMonth()] + " " + now.getDate() + "日 " + dayNames[now.getDay()] + "</FONT>");
document.writeln(" </TH></TR><TR><TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>日</FONT></TH>");
document.writeln(" <th bgcolor='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>一</FONT></TH>");
document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>二</FONT></TH>");
document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>三</FONT></TH>");
document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>四</FONT></TH>");
document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>五</FONT></TH>");
document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>六</FONT></TH>");
document.writeln(" </TR><TR>");
column = 0;
for (i=0; i<startDay; i++) {
document.writeln("\n<TD><FONT STYLE='font-size:9pt'> </FONT></TD>");
column++;
}
for (i=1; i<=nDays; i++) {
if (i == thisDay) {
document.writeln("</TD><TD ALIGN='CENTER' BGCOLOR='#FF8040'><FONT STYLE='font-size:9pt;Color:#ffffff'><B>")
}
else {
document.writeln("</TD><TD BGCOLOR='#FFFFFF' ALIGN='CENTER'><FONT STYLE='font-size:9pt;font-family:Arial;font-weight:bold;Color:#330066'>");
}
document.writeln(i);
if (i == thisDay)
document.writeln("</FONT></TD>")
column++;
if (column == 7) {
document.writeln("<TR>");
column = 0;
}
}
document.writeln("<TR><TD COLSPAN='7' ALIGN='CENTER' VALIGN='TOP' BGCOLOR='#0080FF'>")
document.writeln("<FORM NAME='clock' onSubmit='0'><FONT STYLE='font-size:9pt;Color:#ffffff'>")
document.writeln("現(xiàn)在時(shí)間:<INPUT TYPE='Text' NAME='face' ALIGN='TOP'></FONT></FORM></TD></TR></TABLE>")
document.writeln("</TD></TR></TABLE></div>");
}
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
//顯示當(dāng)前時(shí)間
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = " " + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " 下午 " : " 上午 "
document.clock.face.value = timeValue;
timerID = setTimeout("showtime()",1000);//設(shè)置超時(shí),使時(shí)間動(dòng)態(tài)顯示
timerRunning = true;
}
function startclock () {
stopclock();
showtime();
}
使用頁(yè)面顯示如下:
<html>
<script language="JavaScript" type="text/javascript" src="time.js"> </script>
<body bgcolor="#FFFFFF" onLoad="startclock(); timerONE=window.setTimeout" text="#000000">
<script language="JavaScript" type="text/javascript">
calendar();
</script>
</body>
</html>
這就OK了!
相關(guān)文章
Javascript 世界時(shí)區(qū)時(shí)間顯示代碼
Javascript 世界時(shí)區(qū)時(shí)間顯示代碼,顯示多個(gè)地區(qū)的時(shí)間,需要的朋友可以參考下代碼2012-05-05JavaScript 時(shí)分秒時(shí)間代碼(自動(dòng)補(bǔ)零)
JavaScript 時(shí)分秒時(shí)間代碼,時(shí)間小于10的就補(bǔ)充一個(gè)零。2010-02-02比較簡(jiǎn)潔的JavaScript 實(shí)時(shí)顯示時(shí)間的腳本 修正版
比較簡(jiǎn)潔的JavaScript 實(shí)時(shí)顯示時(shí)間的腳本代碼。大家可以看下,已經(jīng)修改好了。2007-11-11日期處理的js庫(kù)(迷你版)--自建js庫(kù)總結(jié)
先推薦網(wǎng)上不錯(cuò)的日期js庫(kù):http://momentjs.com/ 其實(shí)這類資源網(wǎng)絡(luò)上一抓一把,但是想要針對(duì)項(xiàng)目實(shí)用的還是不多,因?yàn)槲医佑|的那類都經(jīng)常在日期而非深入到hour、minute、second!所以想干脆自己編個(gè)小庫(kù)吧,這樣以后寫代買將省力很多,在這里分享下,希望對(duì)大家有用2011-11-11JavaScript 獲取當(dāng)前時(shí)間戳的代碼
JavaScript 獲取當(dāng)前時(shí)間戳的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-08-08關(guān)于js日期轉(zhuǎn)化為毫秒數(shù)“節(jié)省20%的效率和和節(jié)省9個(gè)字符“問題
最近在看松散耦合可定制百度的開源框架tangram.js目光突然就聚焦在一種獲得毫秒數(shù)的寫法2012-03-03顯示服務(wù)器時(shí)間的在線時(shí)鐘代碼簡(jiǎn)單
不知道這里之前有沒有這種代碼。在網(wǎng)上找了半天也沒有滿意的,于是寫了這段代碼。實(shí)現(xiàn)起來很簡(jiǎn)單,不必重復(fù)讀服務(wù)器時(shí)間,讀一次即可。2008-05-05javascript 當(dāng)前日期轉(zhuǎn)化為中文的實(shí)現(xiàn)代碼
有時(shí)候需要將當(dāng)面的日期,轉(zhuǎn)換成中文,這里是js的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05