Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time range midnight problem
#3
That seems to work just fine.  Good idea!
Here is the code that now works before, after, and encompassing midnight.

Thank you

                tm= datetime.datetime.now()
                now_time = tm.time()
                
                # The next few lines are a test for a range that crosses midnight
                #now_time = datetime.time(23,54) # should fail
                #now_time = datetime.time(23,55) # should passs
                #now_time = datetime.time(0,4) # should fail
                #now_time = datetime.time(0,3) # should pass

                starthr = 23 # befot=re midnight
                startmin = 55
                stophr = 0 # after midnight
                stopmin = 3
                
                if stophr >= starthr:
                    if now_time >= datetime.time(starthr, startmin) and now_time <= datetime.time(stophr,stopmin):
                        print("in the range")
                else:
                    if now_time <= datetime.time(stophr,stopmin) or now_time >= datetime.time(starthr, startmin):
                        print("in the range")    
                    else:
                        print("not in the range")
Reply


Messages In This Thread
time range midnight problem - by PickyBiker - Dec-13-2016, 02:59 AM
RE: time range midnight problem - by Ofnuts - Dec-13-2016, 05:14 PM
RE: time range midnight problem - by PickyBiker - Dec-13-2016, 06:46 PM
RE: time range midnight problem - by sparkz_alot - Dec-13-2016, 07:44 PM
RE: time range midnight problem - by nilamo - Dec-13-2016, 08:25 PM
RE: time range midnight problem - by PickyBiker - Dec-13-2016, 10:53 PM
RE: time range midnight problem - by nilamo - Dec-13-2016, 11:24 PM
RE: time range midnight problem - by Ofnuts - Dec-14-2016, 12:49 AM
RE: time range midnight problem - by PickyBiker - Dec-14-2016, 07:45 PM
RE: time range midnight problem - by Ofnuts - Dec-15-2016, 12:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 675 Nov-10-2023, 08:19 PM
Last Post: snippsat
  Problem with module time and leap seconds Pedroski55 3 1,318 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  matplotlib x axis range goes over the set range Pedroski55 5 3,340 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  how to change the range of read CSV file every time python file runs greenpine 6 4,621 Dec-08-2020, 10:11 PM
Last Post: greenpine
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 4,834 Nov-20-2020, 02:26 PM
Last Post: greenpine
  Writing a function to calculate time range in Unix Epoch Time t4keheart 2 3,095 Jul-15-2020, 01:55 AM
Last Post: t4keheart
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,307 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  time.sleep(...) problem DPaul 3 2,887 Jan-28-2019, 11:40 AM
Last Post: Larz60+
  Would like to input a date variable and determine whether it is within the time range harold 3 2,650 Jan-05-2019, 09:04 AM
Last Post: Gribouillis
  Index Out Of Range problem yelyah 5 3,496 Dec-08-2018, 09:28 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020