DEV Community

Cover image for Python 25: New Year Countdown
Gregor Schafroth
Gregor Schafroth

Posted on

Python 25: New Year Countdown

Alright it’s the last day of the year, so I asked ChatGPT to give me a Python exercises related to that and here is what I got.

You can find my solution below 😊

Exercise: New Year Countdown

Write a Python program that calculates and displays a countdown to the end of the year 2023. The program should:

  1. Calculate the number of days, hours, minutes, and seconds remaining until the end of the year 2023 from the current date and time.
  2. Display the countdown in the following format:
    • "Days: X, Hours: Y, Minutes: Z, Seconds: W"

Your program should use Python's datetime module to work with dates and times. You can calculate the time difference between the current date and time and the end of the year 2023, and then format the result to display the countdown.

Below is my solution. I just spruced it up a bit by displaying the result as a pyfiglet and wishing the user a happy new year if it’s already 2024. As always I used my beloved logging module, even tho it does not show anything in this code when the level is set to ERROR. Change this to DEBUG if you want to see the messages 🙂

import datetime
import logging
import pyfiglet

logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s: %(message)s')

def time_until_2024():
    """
    Calculates the number of days, hours, minutes, and seconds remaining until the end of the year 2023 from the current date and time.
    """
    logging.debug('time_until_2024()')

    # Get the current date and time
    current_datetime = datetime.datetime.now()

    # Define the end of the year 2023
    end_of_year_2023 = datetime.datetime(2023, 12, 31, 23, 59, 59)

    # Calculate the time difference
    time_difference = end_of_year_2023 - current_datetime

    # Return the time difference
    logging.debug(f'time_until_2024() return: {time_difference}')
    return time_difference

def print_time(time_remaining):
    """
    Display the countdown in the following format: "Days: X, Hours: Y, Minutes: Z, Seconds: W"
    """
    logging.debug(f'print_time({time_remaining})')

    # Create Happy New Year text if it's already 2024
    current_datetime = datetime.datetime.now()
    if current_datetime.year >= 2024:
        print_message = pyfiglet.figlet_format("It's 2024, Happy New Year!")

    else:
        # Extract days, seconds, and microseconds from the time difference
        remaining_days = time_remaining.days
        remaining_seconds = time_remaining.seconds
        remaining_microseconds = time_remaining.microseconds

        # Calculate hours, minutes, and remaining seconds
        remaining_hours, remaining_seconds = divmod(remaining_seconds, 3600)
        remaining_minutes, remaining_seconds = divmod(remaining_seconds, 60)

        # Create stylized text art for the countdown result
        print_message = pyfiglet.figlet_format(f'Days: {remaining_days},\n Hours: {remaining_hours},\n Minutes: {remaining_minutes},\n Seconds: {remaining_seconds}')

    # Display the message
    print(print_message)

def main():
    time_remaining = time_until_2024()
    print_time(time_remaining)
    logging.debug('main() end')

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)