Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Datetime

Is it possible to print a line of a code on a specific date?

1 Answer

Yes

from datetime import date
import time

while True:
    today = date.today()

    # mm/dd/y
    # 10/27/2020
    today_date = today.strftime("%m/%d/%y")    

    if str(today_date) == "10/28/2020":
        print("Message")
    # Sleeps for 1 second then checks again
    time.sleep(1)

This will require that your program is kept open and running in the background. Other alternatives are having to use external tools, and they depend on what kind of system you are running.

correction

"10/28/2020" should be "10/28/20"