为什么 datetime.strptime 在这个简单的例子中不起作用? [英] Why is datetime.strptime not working in this simple example?

查看:49
本文介绍了为什么 datetime.strptime 在这个简单的例子中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

这篇关于为什么 datetime.strptime 在这个简单的例子中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆