Creating Digital Clock Using Date Shower in Python

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