【发布时间】:2016-06-26 21:43:51
【问题描述】:
我正在使用此代码:
def calcDateDifferenceInMinutes(end_date,start_date):
fmt = '%Y-%m-%d %H:%M:%S'
start_date_dt = datetime.strptime(start_date, fmt)
end_date_dt = datetime.strptime(end_date, fmt)
# convert to unix timestamp
start_date_ts = time.mktime(start_date_dt.timetuple())
end_date_ts = time.mktime(end_date_dt.timetuple())
# they are now in seconds, subtract and then divide by 60 to get minutes.
return (int(end_date_ts-start_date_ts) / 60)
来自这个问题:stackoverflow question
但我收到了这条消息:
AttributeError: 'str' 对象没有属性 'datetime'
我已经查看了类似的问题,但除了执行以下操作之外没有其他选择:
start_date_dt = datetime.datetime.strptime(start_date, fmt)
这是完整的跟踪:
> Traceback (most recent call last): File "tabbed_all_cols.py", line
> 156, in <module>
> trip_calculated_duration = calcDateDifferenceInMinutes (end_datetime,start_datetime) File "tabbed_all_cols.py", line 41, in
> calcDateDifferenceInMinutes
> start_date_dt = datetime.datetime.strptime(start_date, fmt) AttributeError: 'str' object has no attribute 'datetime'
第 41 行是:
start_date_dt = datetime.datetime.strptime(start_date, fmt)
有人能解释一下我错过了什么吗?
新更新:我仍在努力解决这个问题。我看到那个版本很重要。我正在使用 2.7 版并正在导入日期时间。
我不认为我将字符串日期设置回字符串,这是我认为人们在下面建议的。
谢谢
【问题讨论】:
-
上游某处,你一定做过
datetime = "some string" -
你的标题和你的问题有不同的错误信息。是哪个?
-
大家好:这是对方法的调用:duration = calcDateDifferenceInMinutes (tend_datetime,start_datetime)。我将这些作为字符串传递。
-
请显示完整的堆栈跟踪。它会告诉你哪一行有错误
-
'@zondo - 是的,我已经更新了我的标题以反映我遇到的错误。感谢您了解这一点。