-1

I’m working on a project that will run at different times of the day. And I want it to do different things at those different times. How to I write a conditional statement based on the time?

For example:

How would I write

If time == 8AM:
    do this
Elif time == 10PM:
    do this
tdelaney
  • 73,364
  • 6
  • 83
  • 116

1 Answers1

0

Using datetime is an option:

import datetime as dt

# Store the current hour (0-23)
current_hour = dt.datetime.now().hour

# Check if current hour is 8AM or 10PM
if current_hour == 8:
    # Do this at 8 AM
    pass
elif current_hour == 22:
    # Do this at 10 PM (22h)
    pass
Honey Gourami
  • 150
  • 11