Skip to main content

How to Create a Digital Clock Display in Batch Script

In an automated environment, such as a data center monitoring terminal or a retail kiosk, having a prominent live clock on the screen can be extremely useful. While the Windows taskbar has a clock, a Digital Clock Display inside your Batch script provides a central focus point and ensures the time is logged alongside your script's actions.

In this guide, we will demonstrate how to create a "Live" digital clock that updates every second in your terminal window.

The Strategy: The Overwrite Loop

A digital clock in Batch works by:

  1. Positioning the cursor at a fixed location (or clearing the screen).
  2. Displaying the current value of the !TIME! variable.
  3. Waiting for approximately 1 second.
  4. Repeating the process.

Method 1: The Simple Full-Screen Clock

This script creates a centered, large digital clock that takes over the entire terminal.

Implementation Script

@echo off
setlocal EnableDelayedExpansion

:: ----------------------------
:: 1. Minimal safe PATH
:: ----------------------------
set "PATH=%SystemRoot%\System32;%SystemRoot%"

:: ----------------------------
:: 2. Capture ESC character
:: ----------------------------
for /f %%A in ('echo prompt $E ^| cmd') do set "ESC=%%A"
if not defined ESC (
echo [ERROR] Could not generate ANSI escape character.
pause
exit /b 1
)

:: ----------------------------
:: 3. ANSI Colors
:: ----------------------------
set "C_TIME=!ESC![92m"
set "C_RESET=!ESC![0m"

title Digital Clock v1.3
cls

:clock_loop
:: ----------------------------
:: 4. Get current time with leading zero fix
:: ----------------------------
set "t=%TIME%"
if "!t:~0,1!"==" " set "t=0!t:~1!"
set "currentTime=!t:~0,8!"

:: ----------------------------
:: 5. Move cursor to top-left safely
:: ----------------------------
<nul set /p "=!ESC![H"

:: ----------------------------
:: 6. Print clock
:: ----------------------------
echo.
echo.
echo ===================================
echo.
echo CURRENT SYSTEM TIME:
echo.
echo !C_TIME!!currentTime!!C_RESET!
echo.
echo.
echo ===================================
echo.
echo Press Ctrl+C to stop.

:: ----------------------------
:: 7. Delay 1 second
:: ----------------------------
%SystemRoot%\System32\timeout.exe /t 1 /nobreak >nul

goto clock_loop

Method 2: The "Corner" Clock (Dashboard Style)

Often, you don't want the clock to take over the entire screen. Using ANSI coordinates, you can place a clock in the top-right corner that updates continuously without affecting the rest of the terminal content.

@echo off
setlocal EnableDelayedExpansion

:: Enable ANSI/VT100 escape sequences
for /f %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"

:: Clear screen once
cls

:: Print static logs (these stay put and don't redraw)
echo [LOG] Initializing system sensors...
echo [LOG] Monitoring active connections...
echo [LOG] Press Ctrl+C to stop.

:loop
:: Save cursor position
<nul set /p "=%ESC%[s"

:: Move cursor to row 1, column 60 (top-right corner)
<nul set /p "=%ESC%[1;60H"

:: Print the clock
<nul set /p "=[ %TIME:~0,8% ]"

:: Restore cursor position
<nul set /p "=%ESC%[u"

:: Wait 1 second
timeout /t 1 /nobreak >nul

goto loop

How the corner clock works:

  • !ESC![s: Saves the current cursor position so we can return to it after updating the clock.
  • !ESC![1;60H: Jumps to row 1, column 60 to draw the clock in the upper-right area.
  • !ESC![K: Clears to the end of the line to remove any leftover characters from the previous time string.
  • !ESC![u: Restores the cursor to the saved position, so any future output appears below the log content rather than at the clock position.

Creating a 12-Hour vs. 24-Hour Clock

By default, !TIME! uses your system's regional settings (usually 24-hour format). If you want to force a 12-hour AM/PM display, you need a small conversion:

@echo off
setlocal enabledelayedexpansion

:: Extract the hour and handle the leading space
:: (Some locales use " 9:30" instead of "09:30")
set "hh=!TIME:~0,2!"
set "hh=!hh: =0!"

set "period=AM"
if !hh! geq 12 set "period=PM"
if !hh! gtr 12 set /a "hh-=12"
if !hh! equ 0 set "hh=12"

:: Pad single-digit hours with a leading space for alignment
if !hh! lss 10 set "hh= !hh!"

echo Time: !hh!:!TIME:~3,2!:!TIME:~6,2! !period!

endlocal
pause
info

Note on leading spaces: Some Windows locales format single-digit hours with a leading space instead of a leading zero (e.g., 9:30:00 instead of 09:30:00). The line set "hh=!hh: =0!" replaces any space with a zero to normalize the value before performing arithmetic comparisons, which would fail on 9 (a space followed by a digit).

Best Practices for Digital Clocks

  1. Avoid cls Flicker: Using cls in a tight loop causes the entire screen to flash white/black on each iteration. Instead, use ANSI positioning (ESC[1;1H) to overwrite content in place, or ESC[J to clear from the cursor down.
  2. Handle Leading Spaces: If the hour is before 10 AM, !TIME! may start with a space (e.g., 9:30:00) depending on the locale. Normalize this before using the value in arithmetic or fixed-width displays.
  3. Independent Clock Process: If your script performs heavy work (like copying large files), the clock will freeze until that command finishes, since Batch is single-threaded. For a truly independent clock, run it in a separate process: start "Clock" cmd /c clock.bat.
  4. Title Bar Clock: You can display the time in the window's title bar using title !TIME:~0,8! inside the loop, which keeps the main screen completely free for data output.

Conclusion

A digital clock is more than just a novelty; it is a functional UI element that provides real-time context to your automation tools. Whether you use a full-screen display for a status kiosk or a subtle corner clock for an administrative dashboard, the use of loops and ANSI positioning allows you to turn a static Batch file into a live monitoring environment.