Getting the closest date to a given date

前端 未结 8 624
Happy的楠姐
Happy的楠姐 2020-12-03 08:50

Given this base date:

base_date = \"10/29 06:58 AM\"

I want to find a tuple within the list that contains the closest date to the bas

8条回答
  •  鱼传尺愫
    2020-12-03 09:06

    import datetime
    
    fmt = '%m/%d %H:%M %p'
    d = datetime.datetime.strptime(base_date, fmt)
    def foo(x):
       return (datetime.datetime.strptime(x[0],fmt)-d).total_seconds() > 0
    sorted(list_date, key=foo)[-1]
    

提交回复
热议问题