how to compare strftime values

前端 未结 3 600
醉话见心
醉话见心 2021-01-14 07:54

I have two values created from strftime as below

TIMEFORMAT=\"%Y-%m-%d %H:%M:%S\"

time1 = time.strftime(TIMEFORMAT)
time2 = time.strftime(TIMEFORMAT)
         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-14 08:28

    Hope this helps you:

    In [1]: from datetime import datetime, timedelta
    
    In [2]: nd = datetime.now()
    
    In [3]: pd = nd - timedelta(hours=1)
    
    In [4]: snd = nd.strftime("%Y-%m-%d %H:%M:%S")
    
    In [5]: spd = pd.strftime("%Y-%m-%d %H:%M:%S")
    
    In [6]: print(snd, spd)
    ('2013-11-22 15:21:22', '2013-11-22 14:21:22')
    
    In [7]: datetime.strptime(snd, "%Y-%m-%d %H:%M:%S") > datetime.strptime(spd, "%Y-%m-%d %H:%M:%S")
    Out[7]: True
    

提交回复
热议问题