実行結果
0時間 0分 0秒(2023年9月5日18:50から)
プログラム
<!DOCTYPE html>
<html>
<head>
<title>経過時間表示</title>
</head>
<body>
<p id="timer">0時間 0分 0秒</p>
</body>
<script>
// 開始時刻を設定します(例:2023年1月1日の0時0分0秒)
const startTime = new Date("2023-09-05T18:50:00").getTime();
function updateTimer() {
const currentTime = new Date().getTime();
const elapsedTime = currentTime - startTime;
const hours = Math.floor(elapsedTime / 3600000); // 1時間 = 3600000ミリ秒
const minutes = Math.floor((elapsedTime % 3600000) / 60000); // 1分 = 60000ミリ秒
const seconds = Math.floor((elapsedTime % 60000) / 1000); // 1秒 = 1000ミリ秒
document.getElementById("timer").textContent = `${hours}時間 ${minutes}分 ${seconds}秒`;
}
// タイマーを1秒ごとに更新
setInterval(updateTimer, 1000);
</script>
</html>
コメント