Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How do I determine if current time is within a specified range using Python's datetime module?

$
0
0

What would be the best way to see if the current time lies between say 10:30 AM and 4:30 PM.

I could think of the following, not sure how correct:

from datetime import datetimenw = datetime.now()hrs = nw.hour;mins = nw.minute;secs = nw.second;zero = timedelta(seconds = secs+mins*60+hrs*3600)st = nw - zero # this take me to 0 hours. time1 = st + timedelta(seconds=10*3600+30*60) # this gives 10:30 AMtime2 = st + timedelta(seconds=16*3600+30*60)  # this gives 4:30 PMif nw>= time1 or nw <= time2:    print "yes, within the interval"

Please let me know if this the correct approach, can something better be written?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>