简体   繁体   中英

Compare the time of datetime.now() object with time string

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?

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, eg

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)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM