[Python] I want to create a code that only works for a certain period of time.

Asked 2 years ago, Updated 2 years ago, 17 views

Hello, I'm a beginner at Python. I'm trying to create a code that works for a specified period of time or only for a specified period of time as the title suggests, but I don't know how to use the time function.

Set Start Time (Date)

Code Execution

Set end time (date)

I want to do it in this format, but how do I apply it as a time or datetime function?

python

2022-09-22 21:08

1 Answers

How about using the early return method?

import datetime

def works_until_2018():
    last_day_2018 = datetime.datetime(2018, 12, 31)
    if datetime.datetime.now() > last_day_2018:
        return 
    print("This code will only work until 2018."")
    print ("It's still 2018")

works_until_2018()


2022-09-22 21:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.