'Jun 1 2005 1:33PM' 转换为datetime object
2014-10-06 09:39
555 查看
stackoverflow 上有一个这样的问题(点击打开链接):
如何将下列样式的字符串转换为datetime object
这里需要注意一个地方,小时(Hour)我们一般用%H表示,但是我们使用%p匹'PM',而使用这个的前提是,hour必须是12小时制,所以使用%I
(2)可是不是要求转化为datetime object吗?目前的格式是time.strut_time,这样保存进数据库是不行的.
官方文档关于(datetime.strptime)的解释点击打开链接
如何将下列样式的字符串转换为datetime object
Jun 1 2005 1:33PM Aug 28 1999 12:00AM(1)上面的答案是这样的:
import time stamp = 'Jun 1 2005 1:33PM' timeobj = time.strptime(stamp, '%b %d %Y %I:%M%p')得到的是一个time.strut_time()对象:
>>>print timeobj struct_time(tm_year=2005, tm_mon=6, tm_mday=1, tm_hour=13, tm_min=33, tm_sec=0, tm_wday=2, tm_yday=152, tm_isdst=-1) >>>type (timeobj) <type 'time.struct_time'>官方文档关于(time.strptime)的解释点击打开链接
这里需要注意一个地方,小时(Hour)我们一般用%H表示,但是我们使用%p匹'PM',而使用这个的前提是,hour必须是12小时制,所以使用%I
When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
(2)可是不是要求转化为datetime object吗?目前的格式是time.strut_time,这样保存进数据库是不行的.
>>> datetimeobj = datetime.strptime(s,'%b %d %Y %I:%M%p') >>> print datetimeobj 2005-06-01 13:33:00 >>> type(datetimeobj) <type 'datetime.datetime'>这样就可以了
官方文档关于(datetime.strptime)的解释点击打开链接
相关文章推荐
- 'CFile:: Open' : 无法将参数 1从 'const char [11]' 转换成'LPCTSTR'
- java中转换英语日期为常用格式,例:"Jun 20, 2014 8:56:14 PM";
- 未能找到存储过程 'sys.sp_addextendedproperty' SQL2005-2000
- 'A,B,C'字符串转换为列形式
- [Collect]Something about IECT'2005
- Cannot convert type 'ASP.login_aspx' to 'System.Web.UI.WebControls.Login'(无法将类型'ASP.login_aspx' 转换为'System.Web.UI.WebControls.Lo
- Visual studio 2005中调试遇到 EXECUTE permission denied on object 'sp_sdidebug', database 'master', owner 'dbo'
- java 去掉字符串中的'/'字符
- "An extender can't be in a different UpdatePanel than the control it extends." error really needs debugging information ,错误
- 怎样安装ASP.NET 'Atlas' Atlas vs.net 2005
- 'Sys' is undefined 错误 -- 使用ASP.NET 2.0 AJAX Extensions
- 执行sp_helpdb提示不能向表的'owner'列插入空值
- CLI 连接失败。SQL0332N 没有从源代码页 "1114" 至目标代码页 "UNKNOWN" 的转换。原因代码是 "1"。 SQLSTATE=57017
- 我理解keso的"东拉西扯:不要听用户的" -对牛乱弹琴 | Playin' with IT
- '__pendingCallbacks[...].async' 错误
- 将 varchar 值 'null' 转换为数据类型为 int 的列时发生语法错误。
- "在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误"的解决方法
- fix error: 'import site' failed;use -v for traceback
- 表tab1和tab2 'StkList_ID'关联 tab2 以'group_id'分组 shape语句怎么写?
- '显示桌面'的内容