■HTMLファイルのHEADタグ内に記述
<meta http-equiv="Content-Type" content="text/html; charset= UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <script type="text/javascript" src="js/clock.js"></script>
■HTMLファイルのBODYタグ内に記述
<span id="clock">0000年00月00日(*) 00:00:00</span>
■jsディレクトリ内のclock.jsの記述
// 変数の定義
var clockSpan, now, days, year, month, date, day, hour, min, sec;
days = new Array("日", "月", "火", "水", "木", "金", "土");
window.onload = setClock;
// 最初の設定
function setClock(){
clockSpan = document.getElementById("clock");
tickTime();
}
// 1秒ごとの処理
function tickTime(){
now = new Date();
year = now.getFullYear();
month = now.getMonth();
date = now.getDate();
day = now.getDay();
hour = now.getHours();
min = now.getMinutes();
sec = now.getSeconds();
clockSpan.innerHTML = year + '年' + month + '月' + date + '日(' + days[day] + ') ' + addZero(hour) + ':' + addZero(min) + ':' + addZero(sec);
// 1秒ごとに処理する場合は、第2引数に1000 と記述
setTimeout("tickTime()",1000);
}
// ゼロ詰め
function addZero(num) {
num = '00' + num;
num = num.substring( num.length - 2, num.length );
return num;
}
こちらもあわせてどうぞ