Creating Digital Clock Using Date Shower in Python
Last Updated :
28 Apr, 2025
Python is a versatile language used for a wide range of applications, including Graphical User Interfaces (GUI) using Tkinter. However, sometimes for simplicity, a command line can be used to create simple applications. Building a Digital Clock with a Date Shower in Python is a perfect example. This article will explore how we can create a Digital Clock With a Date Shower using the command line in Python.
Creating Digital Clock Using Date Shower in Python
To create a digital clock with date shower, we will use Python modules, 'time' and 'os' as these libraries allow us to retrieve the current time and clear the command line output to create an updating display. Below is the step-wise breakdown of the Python code for our digital clock in Python:
Step 1: Importing the libraries
As mentioned above, we will start with importing the standard Python libraries, 'time' and 'os'. Here, the 'time' module is used to access the current time, and 'os' is used to interact with the operating system to clear the screen.
Python3
# importing libraries
import time
import os
Step 2: Clearing the Screen
To create a dynamic digital clock with a date display, we use the clear_screen function to clear the console screen for each update. The function checks the operating system using os.name and executes the 'cls' command for Windows ('nt') or 'clear' for other operating systems to maintain a clean and updated display.
Python3
# defining clear_scree() function
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
Step 3: Main Function
In the main function, an infinite loop continuously updates the time and date using 'time.strftime'. After each update, the 'clear_screen' function is called to clear the console output. The formatted time in both 24-hour and 12-hour formats, along with the formatted date, is then printed. The loop pauses for one second with 'time.sleep(1)' to create a one-second interval between updates.
Python3
# defining main function
def main():
while True:
current_time_24hr = time.strftime("%H:%M:%S")
current_time_12hr = time.strftime("%I:%M:%S %p")
current_date = time.strftime("%Y-%m-%d")
# Clear the screen for a fresh update
clear_screen()
print(f"24-Hour Format: {current_time_24hr}")
print(f"12-Hour Format: {current_time_12hr}")
print(f"Current Date: {current_date}")
Step 4: Execution Block
In the execution block, if __name__ == "__main__":, the code ensures that the main() function runs only when the script is executed directly, allowing the continuous updates of the digital clock with a date to occur.
Python3
# main exectution block
if __name__ == "__main__":
main()
Complete Code
Python3
import time
import os
# Function to clear the console screen
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
# Main execution block
def main():
while True:
current_time_24hr = time.strftime("%H:%M:%S")
current_time_12hr = time.strftime("%I:%M:%S %p")
current_date = time.strftime("%Y-%m-%d")
# Clear the screen for a fresh update
clear_screen()
print(f"24-Hour Format: {current_time_24hr}")
print(f"12-Hour Format: {current_time_12hr}")
print(f"Current Date: {current_date}")
# Pause for one second between updates
time.sleep(1)
# Run the clock
if __name__ == "__main__":
main()
Run the Program
python script_name.py
Output

Similar Reads
Creating a list of range of dates in Python Given a date, the task is to write a Python program to create a list of a range of dates with the next k dates starting from the current date. For example, if the given date is 4th January 1997 and k is 5, the output should be a list containing 4th January 1997, 5th January 1997, 6th January 1997, 7
3 min read
Create a Countdown Timer for New Year Using Python Many of us eagerly wait the arrival of the New Year. A countdown timer is a way to keep track of the remaining time until midnight. To achieve this, we'll utilize Python's datetime and time modules. The datetime module allows us to work with dates and times, while the time module helps in creating d
3 min read
How To Create a Countdown Timer Using Python? In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format 'minutes: seconds'. We will use the time module here.Step-by-Step Approac
2 min read
Convert Epoch Time to Date Time in Python Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially i
3 min read
Convert string to DateTime and vice-versa in Python A common necessity in many programming applications is dealing with dates and times. Python has strong tools and packages that simplify handling date and time conversions. This article will examine how to effectively manipulate and format date and time values in Python by converting Strings to Datet
6 min read
Python | Create an empty text file with current date as its name In this article, we will learn how to create a text file names as the current date in it. For this, we can use now() method of datetime module. The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focu
1 min read
Python - Convert day number to date in particular year Given day number, convert to date it refers to. Input : day_num = "339", year = "2020" Output : 12-04-2020 Explanation : 339th Day of 2020 is 4th December. Input : day_num = "4", year = "2020" Output : 01-04-2020 Explanation : 4th Day of 2020 is 4th January. Method #1 : Using datetime.strptime() In
5 min read
Convert "unknown format" strings to datetime objects in Python In this article, we are going to see how to convert the "Unknown Format" string to the DateTime object in Python. Suppose, there are two strings containing dates in an unknown format and that format we don't know. Here all we know is that both strings contain valid date-time expressions. By using th
3 min read
Python program to print current year, month and day In this article, the task is to write a Python Program to print the current year, month, and day. Approach: In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date classCreate an object of the date clas
1 min read
Python program to print Calendar without calendar or datetime module Given the month and year. The task is to show the calendar of that month and in the given year without using any module or pre-defined functions. Examples: Input : mm(1-12) :9 yy :2010 Output : September 2010 Su Mo Tu We Th Fr Sa 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2
3 min read