Kode JavaScript untuk Membuat Jam Digital - Panduan Lengkap
Berikut adalah contoh kode untuk membuat jam digital dengan JavaScript:
html
<!DOCTYPE html>
<html>
<head>
 <title>Jam Digital</title>
 <style>
 #clock {
 font-size: 48px;
 text-align: center;
 margin-top: 50px;
 }
 </style>
</head>
<body>
 <div id="clock"></div>

 <script>
 function showTime() {
 var date = new Date();
 var h = date.getHours();
 var m = date.getMinutes();
 var s = date.getSeconds();
 var session = "AM";

 if (h == 0) {
 h = 12;
 }

 if (h > 12) {
 h = h - 12;
 session = "PM";
 }

 h = (h < 10) ? "0" + h : h;
 m = (m < 10) ? "0" + m : m;
 s = (s < 10) ? "0" + s : s;

 var time = h + ":" + m + ":" + s + " " + session;
 document.getElementById("clock").innerText = time;
 document.getElementById("clock").textContent = time;

 setTimeout(showTime, 1000);
 }

 showTime();
 </script>
</body>
</html>

Simpan kode di atas dengan ekstensi .html, lalu buka file tersebut di browser. Anda akan melihat jam digital yang terus berjalan dengan format HH:MM:SS AM/PM.

原文地址: https://www.cveoy.top/t/topic/oPZ1 著作权归作者所有。请勿转载和采集!