[Python] Converting fetched string to datetime in python [closed]

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Selenium project. I in a stage where I'm able to extract the data from a table and formatted the last seen date to i.e. 04-07-25 (%m-%d-%y). What I need to do is to compare this with the current date. Screenshot of the page

    I was able convert the datetime.now to <class 'datetime.time'> but the fetched last seen date is still showing up as a string using the same method bellow. To be able to compare them I need to bring the two variables to the same class.

    current_time = datetime.now() # current time
    now= current_time.strftime("%m-%d-%y")
    print(now)
    ntime = datetime.strptime(now, "%m-%d-%y").time()
    print(type(ntime))


    for t in table:

    last_seen_month = str(t.text[0:1]).zfill(2)
    last_seen_day = str(t.text[2:3]).zfill(2)
    last_seen_year = t.text[4:6]
    last_seen_date = last_seen_month + "/" + last_seen_day + "/" + last_seen_year
    last_seen_date = last_seen_date.replace("/","-")
    print(f'This is the last seen date:{last_seen_date}')
    l_seen = datetime.strptime(last_seen_date, "%m-%d-%y").time()

    print(type(l_seen))
    print(l_seen)


    ntime is ok. Gives back 04-08-25 <class 'datetime.time'> but the for cycle doesn't work because it shows up as 00:00:00

    This is the last seen date:04-08-25
    <class 'str'>
    <class 'datetime.time'>
    00:00:00


    Obviously there is a more neater and tidier way to do this but I'm not a black belt programmer just an enthusiast who wants something to achieve. :)

    Thanks for any answres

    Continue reading...

Compartilhe esta Página