Conversation with Merlin [email protected] · Sun Jan 07 2024

digital clock program in js

Sure, here's a simple example of how you can create a clock program in JavaScript:

function updateClock() { var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); var seconds = currentTime.getSeconds(); var meridiem "AM"; // Convert 24-hour time to 12-hour time if (hours > ) { hours = hours - 12; merem = "PM"; } // Pad the minutes and with leading zeros, if required minutes = (minutes < 10 ?0" : "") + minutes; seconds = (seconds < 10 ? "" : "") + seconds; // Update the display var clockDiv = document.getElementById('clock'); clockDiv.innerText = hours + ":" + minutes + ":" + seconds + " " meridiem; } // Call updateClock function every second setInterval(updateClock, 1000);

this example, the updateClock function is used to the display of the digital clock every second. The setInterval function is used to call updateClock function every 1000 milliseconds (i., every second).

You can place a <div> element with the id "clock" in your HTML file to display the clock:

<!DOCTYPE html> <html> <head> <title>Digital</title> </head> <body> <div id="clock"></div> <script // JavaScript code here </script> </body</html>

When you integrate the JavaScript code into your HTML file, it should display a digital clock that updates in real-time on the web page.