|
演示一:動(dòng)態(tài)的時(shí)鐘(來個(gè)復(fù)雜的)
11:55:13
演示二:顯示完整的一些方法(事實(shí)上我很討厭有些格式了)
Mon Oct 1 22:35:25 UTC+0800 2007
從1970-01-01到現(xiàn)在共過了1191249325859毫秒
返回當(dāng)前的年份2007
2007
返回當(dāng)前月91因?yàn)樵率?-11,所以要加1
返回當(dāng)前日期1
返回當(dāng)前星期1
返回當(dāng)前小時(shí)22
返回當(dāng)前分鐘35
返回當(dāng)前的秒25
Wed Aug 16 11:55:03 UTC+0800 2006
從1970-01-01到現(xiàn)在共過了1155700503156毫秒
返回當(dāng)前的年份2006
2006
返回當(dāng)前月71因?yàn)樵率?-11,所以要加1
返回當(dāng)前日期16
返回當(dāng)前星期3
返回當(dāng)前小時(shí)11
返回當(dāng)前分鐘55
返回當(dāng)前的秒3
演示三: 倒計(jì)時(shí)
距2006年新年還有90天01小時(shí)24分34秒! 距2006年新年還有136天12小時(shí)04分56秒!
<%@LANGUAGE="JavaSCRIPT" CODEPAGE="936"%>
<!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>js核心對(duì)象之Date</title>
<script type="text/Javascript">
function startTime()
{
var today=new Date()//定義一個(gè)時(shí)間對(duì)象
var h=today.getHours()//定義小時(shí)
var m=today.getMinutes()//定義分鐘
var s=today.getSeconds()//定義秒
// add a zero in front of numbers<10
m=checkTime(m)//把分給checkTime處理
s=checkTime(s)//把秒給checkTime處理
document.getElementById('txt').innerHTML=h+":"+m+":"+s//在層txt中顯示
t=setTimeout('startTime()',500)//關(guān)鍵的一句,美隔500毫秒運(yùn)行一次本函數(shù)
}
function checkTime(i)
{//這函數(shù)意思很簡(jiǎn)單了,就是要實(shí)現(xiàn)01-09的效果
if (i<10) //
{i="0" + i}
return i
}
</script>
</head>
<body onload="startTime()">
<p>時(shí)間對(duì)象作為非常重要的一個(gè)對(duì)象,對(duì)我們學(xué).NET的人來說,并不是很重要,但這并不意味著我們可以忽略,事實(shí)上,用得著的時(shí)候還是很多的,如果完全依賴JS處理時(shí)間,那是會(huì)出問題的,因?yàn)镴S總是假設(shè)本地機(jī)器上的時(shí)間是正確的。還有個(gè)原因,他總按照GTM市區(qū)來計(jì)量。我們只是返回當(dāng)前date對(duì)象的副本,我們即便是修改,那也不會(huì)對(duì)對(duì)象本身有任何影響。</p>
<p><strong>演示一:動(dòng)態(tài)的時(shí)鐘</strong>(來個(gè)復(fù)雜的)</p>
<div id="txt"></div>
<p><strong>演示二:顯示完整的一些方法(事實(shí)上我很討厭有些格式了)</strong></p>
<p>
<script language="Javascript">
var md=new Date()
document.write(md+"<br>")
document.write("從1970-01-01到現(xiàn)在共過了"+md.getTime()+"毫秒<br>")
document.write("返回當(dāng)前的年份"+md.getYear()+"<br>")
document.write(md.getFullYear()+"<br>")
document.write("返回當(dāng)前月"+md.getMonth()+1+"因?yàn)樵率?-11,所以要加1<br>")
document.write("返回當(dāng)前日期"+md.getDate()+"<br>")
document.write("返回當(dāng)前星期"+md.getDay()+"<br>")
document.write("返回當(dāng)前小時(shí)"+md.getHours()+"<br>")
document.write("返回當(dāng)前分鐘"+md.getMinutes()+"<br>")
document.write("返回當(dāng)前的秒"+md.getSeconds()+"<br>")
</script>
</p>
<p><strong>演示三: 倒計(jì)時(shí)</strong></p>
<p>
<SCRIPT LANGUAGE="JavaScript">
today = new Date();//申明一個(gè)時(shí)間對(duì)象
intDate = today.getDate();//返回當(dāng)前的天日期
intHours = today.getHours();//返回當(dāng)前小時(shí)
intMinutes = today.getMinutes();//分鐘
intSeconds = today.getSeconds();//秒
intMonth = today.getMonth()+1 ;//月加1
intYear = today.getYear();//返回年
//以下是為了得到0時(shí)0分0秒的差數(shù)
hours = intHours;
hours = (23 - hours);
minutes = intMinutes;
minutes = (59 - minutes);
seconds = intSeconds;
seconds = (59 - seconds);
if (intYear % 4 == 0 && intYear % 100 != 0 || intYear % 400 == 0)
//如果當(dāng)前年除以4余數(shù)為0 同時(shí) 當(dāng)前年初與100 余數(shù)不為0 或者 當(dāng)前年除以400余數(shù)為0,那么本年為366天
{ if (intMonth == 1) {month = "距2006年新年還有"; date = (366 - intDate);}
//以下與本句同意思:用余下的天數(shù)減去當(dāng)前的日期號(hào)數(shù)例如下句,因?yàn)槭嵌拢灾挥?35天,減當(dāng)前天的號(hào)數(shù)
if (intMonth == 2) {month = "距2006年新年還有"; date = (335 - intDate);}
}
else//否則為365天
{ if (intMonth == 1) {month = "距2006年新年還有"; date = (365 - intDate);}
if (intMonth == 2) {month = "距2006年新年還有"; date = (334 - intDate);}
}
if (intMonth == 3) {month = "距2006年新年還有"; date = (304 - intDate);}
if (intMonth == 4) {month = "距2006年新年還有"; date = (273 - intDate);}
if (intMonth == 5) {month = "距2006年新年還有"; date = (243 - intDate);}
if (intMonth == 6) {month = "距2006年新年還有"; date = (212 - intDate);}
if (intMonth == 7) {month = "距2006年新年還有"; date = (182 - intDate);}
if (intMonth == 8) {month = "距2006年新年還有"; date = (152 - intDate);}
if (intMonth == 9) {month = "距2006年新年還有"; date = (121 - intDate);}
if (intMonth == 10) {month = "距2006年新年還有"; date = (91 - intDate);}
if (intMonth == 11) {month = "距2006年新年還有"; date = (60 - intDate);}
if (intMonth == 12) {month = "距2006年新年還有"; date = (30 - intDate);}
//以下當(dāng)然意思有所變了,但是一下的 天,時(shí) ,分,秒 意思差不錯(cuò)了
if (date == 1 ){date = ("0"+date+"天 ");}//如果上面的date得1,那就在前面加個(gè)0
if (date != 1 && date < 10 && date >=0){date = ("0"+date+"天");}//如果不等于1且小于10,同時(shí)大于等于0 都加個(gè)0
if (date > 9){date = (date+"天");}//如果大于9就不用加了
if (hours ==1 ){hours = ("0"+hours+"小時(shí)");}
if (hours != 1 && hours < 10){hours = ("0"+hours+"小時(shí)");}
if (hours > 9){hours = (hours+"小時(shí)");}
if (minutes == 1){minutes = ("0"+minutes+"分 ");}
if (minutes != 1 && minutes < 10){minutes = ("0"+minutes+"分");}
if (minutes > 9){minutes = (minutes+"分");}
if (seconds == 1){seconds = ("0"+seconds+"秒 "+"!");}
if (seconds != 1 && seconds < 10){seconds = ("0"+seconds+"秒!");}
if (seconds > 9){seconds = (seconds+"秒!");}
//如果天小于0,那表示新年到了薩
if (date < 0){month = "Happy";date = " New year!";hours = " 新年";minutes = "快樂";seconds = "!";}
//下面是組合所有的值,簡(jiǎn)單吧
timeString = month+date+hours+minutes+seconds;
document.write(timeString)
</script>
</p>
</body>
</html>
JavaScript技術(shù):javascript入門&#183;動(dòng)態(tài)的時(shí)鐘,顯示完整的一些方法,新年倒計(jì)時(shí),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。