1、日期對象(Date)
var date = new Date(); //獲取到當前的系統時間 document.write("年:"+ date.getFullYear()+"<br/>"); document.write("月:"+ (date.getMonth()+1)+"<br/>"); document.write("日:"+ date.getDate()+"<br/>"); document.write("時:"+ date.getHours()+"<br/>"); document.write("分:"+ date.getMinutes()+"<br/>"); document.write("秒:"+ date.getSeconds()+"<br/>"); //xxxx年yy月dd日 hh:mm:ss document.write("當前時間是:"+date.toLocaleString()); <br/> document.write("當前時間是:"+date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日 "+ date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
其中獲取年份如果采用getYear()返回的是從1900到今年的差值;而getFullYear()是返回今年
toLocaleString()是返回這種格式
Data中不存在優化時間顯示格式的方式,都必須自己修改
2、獲取當期系統時間
2.1、之所以把js腳本寫在下面,是因為瀏覽器在解析html文件時,是按照從前往后的順序解析的,當要調用getCurrentTime()函數時,發現並沒有“time”這個參數,報錯。
2.2 spanObj.innerHTML是往標簽中寫內容
2.3window.setInterval(參數一,參數二); /* setInterval 定時方法,第一個參數要指定調用的代碼(也是字符串方式傳入),第二參數是每隔指定的毫秒數調用指定的代碼。*/
<!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>無標題文檔</title> </head> <body> 當前系統時間:<span id="time"></span> </body> <script type="text/javascript"> function getCurrentTime(){ //獲取到當前的系統時間 var date = new Date(); //把當前系統時間拼裝成我指定的格式。 var timeInfo = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日 "+ date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(); //找span對象 var spanObj = document.getElementById("time"); //設置span標簽體的內容 spanObj.innerHTML = timeInfo.fontcolor("red"); } getCurrentTime(); //定時方法. window.setInterval("getCurrentTime()",1000); /* setInterval 定時方法,第一個參數要指定調用的代碼,第二參數是每隔指定的毫秒數調用指定的代碼。*/ </script> </html>
