Can't compare naive and aware datetime.now() <= challenge.datetime_end

后端 未结 9 1274
离开以前
离开以前 2020-12-02 07:58

I am trying to compare the current date and time with dates and times specified in models using comparison operators:

if challenge.datetime_start <= datet         


        
9条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 08:39

    It is working form me. Here I am geeting the table created datetime and adding 10 minutes on the datetime. later depending on the current time, Expiry Operations are done.

    from datetime import datetime, time, timedelta
    import pytz
    

    Added 10 minutes on database datetime

    table_datetime = '2019-06-13 07:49:02.832969' (example)

    # Added 10 minutes on database datetime
    # table_datetime = '2019-06-13 07:49:02.832969' (example)
    
    table_expire_datetime = table_datetime + timedelta(minutes=10 )
    
    # Current datetime
    current_datetime = datetime.now()
    
    
    # replace the timezone in both time
    expired_on = table_expire_datetime.replace(tzinfo=utc)
    checked_on = current_datetime.replace(tzinfo=utc)
    
    
    if expired_on < checked_on:
        print("Time Crossed)
    else:
        print("Time not crossed ")
    

    It worked for me.

提交回复
热议问题