问题描述
我正在使用 strptime 来转换日期字符串进入 datetime.根据链接页面,这样的格式应该可以工作:
i'm using strptime to convert a date string into a datetime. according to the linked page, formatting like this should work:
>>> # using datetime.strptime() >>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %h:%m")
我的代码是:
import datetime dtdate = datetime.strptime(sdate,"%m/%d/%y")
其中 sdate = "07/27/2012".(我从同一页了解到,%y 是 以世纪为十进制数的年份.")
where sdate = "07/27/2012". (i understand, from the same page, that %y is "year with century as a decimal number.")
我已经尝试将 sdate 的实际值放入代码中:
i have tried putting the actual value of sdate into the code:
dtdate = datetime.strptime("07/27/2012","%m/%d/%y")
但这不起作用.我得到的错误是:
but this does not work. the error i get is:
attributeerror: 'module' 对象没有属性 'strptime'
attributeerror: 'module' object has no attribute 'strptime'
我做错了什么?
推荐答案
你应该使用 datetime.datetime.strptime.请注意,非常旧的 python 版本(2.4 和更早版本)没有 datetime.datetime.strptime;在这种情况下使用 time.strptime.
you should be using datetime.datetime.strptime. note that very old versions of python (2.4 and older) don't have datetime.datetime.strptime; use time.strptime in that case.