Continuous Date and Time - Python Programming Exercise

In this exercise, you will develop a Python program that continuously displays the current date and time in real-time. This exercise is perfect for practicing date and time manipulation, real-time updates, and user interaction in Python. By implementing this program, you will gain hands-on experience in handling date and time operations, real-time updates, and user interaction in Python. This exercise not only reinforces your understanding of date and time manipulation but also helps you develop efficient coding practices for managing user interactions.

 Category

Using Extra Libraries

 Exercise

Continuous Date And Time

 Objective

Develop a Python program that continuously displays the current date and time in real-time. The program should update the display every second and allow the user to stop it with a specific command or after a set period. Implement proper formatting for the date and time, and ensure that the program runs efficiently without excessive resource usage.

 Example Python Exercise

 Copy Python Code
import time
from datetime import datetime

def display_current_time():
    """Function to display the current date and time every second."""
    try:
        while True:
            # Get the current time and format it
            current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            print(f"Current Date and Time: {current_time}", end="\r")  # Overwrite the line in the terminal
            time.sleep(1)  # Wait for 1 second before updating

    except KeyboardInterrupt:
        print("\nProgram stopped by user.")
    except Exception as e:
        print(f"An error occurred: {e}")

def main():
    """Main function to control the display of current time."""
    # Ask the user if they want to start the real-time display
    print("Press Ctrl+C to stop the program or let it run for a set period.")
    
    # Start displaying the current time
    display_current_time()

if __name__ == "__main__":
    main()

 Output

Press Ctrl+C to stop the program or let it run for a set period.
Current Date and Time: 2024-12-27 14:35:52
Current Date and Time: 2024-12-27 14:35:53
Current Date and Time: 2024-12-27 14:35:54
...

 Share this Python Exercise

 More Python Programming Exercises of Using Extra Libraries

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Sitemap Generator

    In this exercise, you will develop a Python program that generates a sitemap for a website. This exercise is perfect for practicing web crawling, URL retrieval...

  •  Generating a List of Images as HTML

    In this exercise, you will develop a Python program that generates an HTML file displaying a list of images from a specified directory. This exercise is perfec...

  •  Retrieving System Information

    In this exercise, you will develop a Python program that retrieves and displays system information, such as the operating system, CPU details, memory usage, and disk ...

  •  Sitemap Generator V2

    In this exercise, you will develop an enhanced version of a sitemap generator in Python (Sitemap Generator V2). This exercise is perfect for practicing web cra...

  •  Exploring a Directory

    In this exercise, you will develop a Python program that explores a specified directory and lists all of its contents, including files and subdirectories. This exe...

  •  Exploring Subdirectories

    In this exercise, you will develop a Python program that explores a specified directory and lists all the subdirectories within it. This exercise is perfect fo...