0

Using Rouble's suggestion and Maulik Gangani's example based on that suggestion:

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

I have two time strings that have been converted into a datetime object using .strptime as follows:

timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M')

I have a datetime object for time now: timeNow = datetime.now()

My problem is that unless I convert timeNow (datetime object) into a string and then back again using .strptime , the script will not work:

timeNow = datetime.now()
timeNow = datetime.strftime(timeNow, '%H%M')
timeNow = datetime.strptime(timeNow, '%H%M')

Full code:

from datetime import datetime

def isNowInTimePeriod(startTime, endTime, nowTime):
    if startTime < endTime:
        return nowTime >= startTime and nowTime <= endTime
    else: #Over midnight
        return nowTime >= startTime or nowTime <= endTime


timeStart = '0300'
timeEnd = '1000'

timeEnd = datetime.strptime(timeEnd, '%H%M')
timeStart = datetime.strptime(timeStart, '%H%M')
timeNow = datetime.now()
timeNow = datetime.strftime(timeNow, '%H%M')
timeNow = datetime.strptime(timeNow, '%H%M')

My question is, how do I format timeNow = datetime.now() so that I can compare it against a string in 24hr format '0000' without having to convert datetime.now() to a string and back again?

Radial
  • 342
  • 1
  • 4
  • 14
  • What I tried is converted my Computer's time format from AM/PM to 24 Hr one, and then did: ```dt.datetime.now()``` which gave me output: ```datetime.datetime(2020, 7, 17, 20, 55, 10, 262243)```. Here clearly it shows date: 17 July, 2020 with 20:55:10:262243 as time in HH:MM:SS format. – JenilDave Jul 17 '20 at 06:28

2 Answers2

1

If you call datetime.strptime, a default date is added if you only supply a time string. datetime.now() will have today's date. You can remove that by using only the time part of the datetime object - which you get by calling the time() method, e.g.

from datetime import datetime

def isNowInTimePeriod(startTime, endTime, nowTime):
    if startTime < endTime:
        return nowTime >= startTime and nowTime <= endTime
    else: #Over midnight
        return nowTime >= startTime or nowTime <= endTime

timeStart = '0300'
timeEnd = '1000'

timeEnd = datetime.strptime(timeEnd, '%H%M').time()
timeStart = datetime.strptime(timeStart, '%H%M').time()
timeNow = datetime.now().time()

print(isNowInTimePeriod(timeStart, timeEnd, timeNow))
# True (my current time is 0823)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
1

The reason why your code is not working, it's because apart from time, datetime (as the name suggests) holds also information about the date.

When you run the following code:

timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M')

The hour and minute is converted, but the rest (the "date" part) is assumed to be epoch:

repr(timeStart)
# Output: 'datetime.datetime(1900, 1, 1, 3, 0)'

When you run datetime.now(), however, that always assumes the current time + date:

>>> datetime.now()
datetime.datetime(2020, 7, 17, 8, 25, 18, 270848)

The reason why converting to string, and back from string works, is because when you convert your datetime to a string with the format '%H%M, you lose the date part in the resulting string. Converting back from that, there's only hour and minutes to read, so you're losing the date part.

Effectivelly, you should be using datetime.time, instead of datetime.datetime to compare time.

After reading using strptime, try using the .time() method to only extract the time part and lose the date part:

timeStart = '0300'
timeStart = datetime.strptime(timeStart, '%H%M').time()

And same for the timeNow part:

timeNow = datetime.now().time()
samu
  • 2,870
  • 16
  • 28