javascript 時間顯示代碼集合(Date對象)_時間日期

來源:互聯網
上載者:User
javascript時間函數
javascript提供了Date對象來進行時間和日期的計算。

Date對象有多種建構函式:

new Date() //目前時間
new Date(milliseconds) //距離起始時間1970年1月1日的毫秒數
new Date(datestring) //字串代表的日期與時間。此字串可以使用Date.parse()轉換,比如"Jannuary 1, 1998 20:13:15"
new Date(year, month, day, hours, minutes, seconds, microseconds) //時間數值,可以不用全部寫,不寫則預設為0


使用時先建立一個Date對象,比如:
dateObj=new Date();

然後調用函數,比如
year=dateObj.getFullYear();//獲得年份值


下面是函數列表。

擷取類函數:
getDate() 函數 -- 返回天數(1-31)
getDay()函數 -- 返回星期數(0-6)
getFullYear() 函數 -- 返回四位元年份
getHours()函數 -- 返回小時數(0-23)
getMilliseconds() 函數 -- 返回毫秒數(0-999)
getMinutes() 函數 -- 返回分鐘數(0-59)
getMonth() 函數 -- 返回月份數(0-11)
getSeconds() 函數 -- 返回的秒數(0-59)
getTime() 函數 -- 返回時間戳記標記法(毫秒錶示)
getYear() 函數 -- 返回年份(真實年份減去1900)


設定類函數:
(以下函數均返回date對象距1970年1月1日午夜之間的毫秒數)
setDate() 函數 -- 設定月份的一天
setFullYear() 函數 -- 設定的年份,月份和天
setHours() 函數 -- 設定小時,分鐘,秒和毫秒
setMilliseconds() 函數 -- 設定毫秒數
setMinutes() 函數 -- 設定分鐘,秒,毫秒
setMonth() 函數 -- 設定月份,天
setSeconds() 函數 -- 設定月份的一天
setTime() 函數 -- 使用毫秒數設定date對象
setYear() 函數 -- 設定年份(真實年份減去1900)


轉化顯示類函數:
toLocalString() 函數 -- 返回本地化字串表示
toLocaleDateString函數 -- 返回日期部分的本地化字串
toLocaleTimeString函數 -- 返回時間部分的本地化字串

相對於local輸出,還有:

toString()
toDateString()
toTimeString()

區別在於前者是根據不同的機器有不同的當地語言格式,後者是內部表示格式


日期解析類函數
Date.parse() 函數 -- 解析一個日期的字串,並返回該日期距1970年1月1日午夜之間的毫秒數

時區相關的部份請看javascript時區函數

javascript顯示時間執行個體
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // 當數字是小於10的就要在前面加0.看起來規範 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>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

JavaScript顯示系統時間帶日期
JavaScript顯示系統時間,顯示年月日時分秒和星期,每秒自動重新整理,並加入日期顏色區別:
星期一至星期五顯示為黑色
星期六顯示為綠色
星期日顯示為紅色
<!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 時間顯示</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>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.