Home>

I am writing a program that checks the next bus from a timetable obtained from the net.
I want to compare the time of the timetable string and the time of the day that can be obtained with datetime
So the time of the string is a time type? I want to convert it to, but the date information is also included.
How can I make comparisons only in time?

Applicable source code
import datetime
#str = ['06: 03 ', '06: 09', '06: 13 ', '06: 21', '06: 26 ', '06: 31', '06: 38 '...]'
now = datetime.datetime.now ()
for i in range (0,83)
    t = datetime.datetime.strptime (str [i], '% H:% M')
    if now<t:
        next = t
    break
print (next)

The date is entered in the above program.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    datetime.datetime.time (hour, minute)


    You can use to create a time object with a specified time only.
    Also, datetime.datetime objects have a time () method,

    now = datetime.datetime.now ()
    now.time ()


    You can get a time object like this.

    In addition, these objects are

    now.hour
    Or
    now.minute
    You can get only hours and minutes like this.