Temple Naylor Temple Naylor - 1 year ago 87
Python Question

How to single out an hour value in Python?

I am trying to create a program that can check to see if businesses in different time-zones are open due to their working ours (9am-9pm). I created functions to get the current time in each time-zone:

Portland_HQ = datetime.datetime.now(tz=pytz.timezone('US/Pacific'))
NewYork_HQ = datetime.datetime.now(tz=pytz.timezone('America/New_York'))
London_HQ = datetime.datetime.now(tz=pytz.timezone('Europe/London'))

I want to be able to extract the hour value of each function so I can create a if statement like so:

if hour >= 9 and hour <= 21:
some code happens..

Edit: I Don't know why I got so many downvotes, I didn't know about that attribute, new to Python and just trying to learn like everyone else. Really enjoy this community but sometimes it can be a little harsh for a beginner. Thank you for everyone that helped! I will use the .hour attribute.

Answer Source

Just check the hour attribute:

if 9 <= Portland_HQ.hour <= 21:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download