How to Create a Simple Digital Clock

84 views

How to Create a Simple Digital Clock

How to Create a Simple Digital Clock

I want to create a digital clock that updates every second. How can I implement this in Python?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

You can use the datetime module to get the current time and the os module to clear the console for a live update.

Code Example:

import datetime
import time
import os

def digital_clock():
    while True:
        now = datetime.datetime.now()
        print(now.strftime("%H:%M:%S"), end='\r')
        time.sleep(1)
        os.system('clear')

digital_clock()

This function displays the current time in hours:minutes:seconds format and updates every second.

solveurit24@gmail.com Changed status to publish February 16, 2025
0