DEV Community

Cover image for A happy new year to everyone in Python!
Everton Tenorio
Everton Tenorio

Posted on

A happy new year to everyone in Python!

Hello devs, may we continue to grow in our tasks and studies throughout 2025. This year, I've learned a lot here on dev.to, so, following the community's premise, Iโ€™m sharing a Python code snippet for those already counting down the hours to the new year.

from datetime import datetime, timedelta

today = datetime.now()
date_format = "%m/%d/%Y"

if today.strftime(date_format) == "12/31/2024":
    print(today.strftime("%H:%M:%S"))
else:
    new_year = today + timedelta(days=1)
    print(f"It's {new_year.strftime('%m/%d/%Y')}. HAPPY NEW YEAR!")
Enter fullscreen mode Exit fullscreen mode

The datetime module is part of Python's standard library and provides classes for manipulating dates and times, such as the datetime class. The timedelta class represents a time difference (duration), like "2 days," "5 hours," or "30 minutes." It is used for operations like adding or subtracting durations from a date/time object. The code checks whether we are on the last day of 2024, and if not...

It's 01/01/2025. HAPPY NEW YEAR! ๐ŸŽ‡ ๐Ÿฅ‚๐Ÿพ

Top comments (0)