Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Javascript – Digital Clock with Style

Posted on December 3, 2024 By David Kittell
<!DOCTYPE html>
<html>
  <head>
    <title>Digital Clock</title>
    <style>
      .content {
        max-width: 500px;
        margin: auto;
      }
      .clock {
        font-size: 24pt;
        font-family: Baskerville, "Palatino Linotype", Palatino,
          "Century Schoolbook L", "Times New Roman", "serif";
        width: 180px;
        border: thick double #ff0004;
        margin: 15px;
        padding: 15px;
      }
    </style>
  </head>
  <body>
    <div id="content" class="content">
      <div id="clock" class="clock"></div>
      <div id="clock24" class="clock"></div>
      <div id="AlertText1" class="clock"></div>
      <div id="AlertText2" class="clock"></div>
    </div>
    <script>
      function showAlert() {
        var myText1 = "Hello world!";

        document.getElementById("AlertText1").textContent = myText1;
        alert(myText1);
        var myText2 = "This can be whatever text you like!";
        alert(myText2);

        document.getElementById("AlertText2").textContent = myText2;
      }

      function updateTime() {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();

        if (minutes < 10) {
          minutes = "0" + minutes; // Add leading zero for minutes
        }
        if (seconds < 10) {
          seconds = "0" + seconds; // Add leading zero for seconds
        }

        var timeOfDay = hours >= 12 && hours < 24 ? "PM" : "AM"; // Determine AM or PM
        if (hours > 12) {
          hours = hours - 12; // Convert to 12-hour format
        }

        if (hours < 10) {
          hours = "0" + hours; // Add leading zero for hours
        }

        // Format the time string
        var time = hours + ":" + minutes + ":" + seconds + " " + timeOfDay;

        // Update the content of the clock div
        document.getElementById("clock").textContent = time;
      }

      function updateTime24() {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();

        if (minutes < 10) {
          minutes = "0" + minutes; // Add leading zero for minutes
        }
        if (seconds < 10) {
          seconds = "0" + seconds; // Add leading zero for seconds
        }

        // var timeOfDay = (hours >= 12 && hours < 24) ? 'PM' : 'AM'; // Determine AM or PM
        //if (hours > 12) {
        //  hours = hours - 12; // Convert to 12-hour format
        // }

        // Format the time string
        //var time = hours + ':' + minutes + ':' + seconds + ' ' + timeOfDay;
        var time = hours + ":" + minutes + ":" + seconds;

        // Update the content of the clock div
        document.getElementById("clock24").textContent = time;
      }

      // Call updateTime immediately when the page loads, and then update every second
      window.onload = function () {
        showAlert();
        updateTime(); // Initial update
        updateTime24(); // Initial update
        setInterval(updateTime, 1000); // Update the time every second
        setInterval(updateTime24, 1000); // Update the time every second
      };
    </script>
  </body>
</html>

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code CSS HTML JavaScript clockjavascripttime

Post navigation

Previous post

Related Posts

MySQL Server Information

Posted on September 11, 2015

SHOW VARIABLES; SHOW STATUS; select version(); SHOW STATUS provides server status information like Connections, Opened_tables, Bytes_received, Bytes_sent, etc. SHOW VARIABLES shows the values of MySQL system variables like time_zone, version, max_connections, etc. References: http://stackoverflow.com/questions/3939803/how-to-get-mysql-server-info-using-command-line All information on this site is shared with the intention to help. Before any source code…

Read More

PowerShell – Get Disk Space Report

Posted on November 9, 2015

<# .SYNOPSIS Drive Space Report .DESCRIPTION Drive Space Report Get-DriveSpace.ps1 [[-computername] <String[]>] [-file <String>] [-Gridview] [-OSOnly] [<CommonParameters>] Produces a simple drive space report for the computer(s) requested .PARAMETER Computername The computer or list of computers that you want the report generated for. if generating the report for multiple computers separate…

Read More

Data Metrics

Posted on September 9, 2013October 26, 2015

In a plan to make metrics easier to record and show I have built my own database tables to record custom metrics. Metrics – Year View C# Metrics Admin Page C# Table Creation SET NOCOUNT ON DECLARE @month INT ,@year INT ,@start_date DATETIME ,@end_date DATETIME SET @month = ( SELECT…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • Front Page
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes