将列表中的日期时间字符串转换为日期时间对象

时间:2019-10-12 08:09:29

标签: python datetime

lstToDt()方法中的第84行,我尝试将需要字符串datetime的'i'从列表转换为datetime对象,但是:

  • 当我使用<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o" crossorigin="anonymous"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <div class="input-group"> <div class="input-group-btn"> <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown"> <img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> <span>One</span> </button> <div class="dropdown-menu"> <a class="dropdown-item sectionScroll" href="#one"><img src="https://i.postimg.cc/44bmkyGL/sdgs-1.jpg"> One</a> <a class="dropdown-item sectionScroll" href="#two"> <img src="https://i.postimg.cc/4NWyy0mp/sdgs-2.jpg"> Two</a> <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/dVD1y9yp/sdgs-3.jpg"> Three</a> <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/pdFL0XkB/sdgs-4.jpg"> Four</a> <a class="dropdown-item sectionScroll" href="#three"> <img src="https://i.postimg.cc/85gPxQ0g/sdgs-5.jpg"> Five</a> </div> </div> </div>时出现错误,我是一个字符串对象,而不是datetime.datetime。
  • 当我使用datetime.datetime.strftime(i,"format")时,出现错误,提示我是datetime.datetime.strptime(i,"format")而不是字符串。

代码:

datetime.datetime

发生了什么事?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您已将字符串值传递给strptime,这对于python是不可接受的,

def lstToDt(lt):      # Converts list string elements into Dates
    for i in lt:
        j = datetime.datetime.strptime(i,"%Y-%m-%d")
        lt.append(datetime.datetime.strptime(j,"%Y-%m-%d"))
    return lt

这应该有效! 希望这会有所帮助!