转自: https://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html

Python格式化日期时间的函数为datetime.datetime.strftime();由字符串转为日期型的函数为:datetime.datetime.strptime(),两个函数都涉及日期时间的格式化字符串,列举如下:

%a Abbreviated weekday name 
%A Full weekday name 
%b Abbreviated month name 
%B Full month name 
%c Date and time representation appropriate for locale 
%d Day of month as decimal number (01 - 31) 
%H Hour in 24-hour format (00 - 23) 
%I Hour in 12-hour format (01 - 12) 
%j Day of year as decimal number (001 - 366) 
%m Month as decimal number (01 - 12) 
%M Minute as decimal number (00 - 59) 
%p Current locale's A.M./P.M. indicator for 12-hour clock 
%S Second as decimal number (00 - 59) 
%U Week of year as decimal number, with Sunday as first day of week (00 - 51) 
%w Weekday as decimal number (0 - 6; Sunday is 0) 
%W Week of year as decimal number, with Monday as first day of week (00 - 51) 
%x Date representation for current locale 
%X Time representation for current locale 
%y Year without century, as decimal number (00 - 99) 
%Y Year with century, as decimal number 
%z, %Z Time-zone name or abbreviation; no characters if time zone is unknown 
%% Percent sign

举一个例子:

ebay中时间格式为‘Sep-21-09 16:34’

则通过下面代码将这个字符串转换成datetime

>>> c = datetime.datetime.strptime('Sep-21-09 16:34','%b-%d-%y %H:%M');
>>> c
datetime.datetime(2009, 9, 21, 16, 34)

又如:datetime转换成字符串

>>> datetime.datetime.now().strftime('%b-%d-%y %H:%M:%S');
'Sep-22-09 16:48:08'

python 处理时间和日期的更多相关文章

  1. python处理时间和日期

    时间和日期  (图中错误修正:dt_obj.strftime(format))     import time, datetime 1. datetime obj 1) datetime dateti ...

  2. 精通 Oracle+Python,第 2 部分:处理时间和日期

    从 Python 2.4 版开始,cx_Oracle 自身可以处理 DATE 和 TIMESTAMP 数据类型,将这些列的值映射到 Python 的 datetime 模块的 datetime 对象中 ...

  3. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  4. Python时间与日期操作(datetime、time、calendar)

    相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...

  5. Python时间戳和日期的相互转换

    Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼   分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...

  6. python中时间日期格式化符号

    python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...

  7. python datetime 时间日期处理小结

    python datetime 时间日期处理小结 转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232. ...

  8. Python时间和日期学习

    #coding=utf-8 __author__ = 'Administrator' #日期和时间模块学习 """ Python程序能用很多方式处理日期和时间,转换日期格 ...

  9. Python学习--15 日期和时间

    获取当前时间 # coding: utf-8 from datetime import datetime now = datetime.now() print(now) print(now.strft ...

随机推荐

  1. java开始到熟悉62

    (说明:昨天网络出现了问题导致昨天的没有按时上传,这篇算是昨天的,今天晚上照常上传今天的内容) 本次主题:数组拷贝.排序.二分法 1.数组拷贝 a.java.lang中System 类包含一些有用的类 ...

  2. 1 npoi 网上 不用模板 设置密码 workbook.WriteProtectWorkbook("password", "admin"); 、、 2 locked.IsLocked = true; sheet1.ProtectSheet("password");NPOI操作EXCEL--设置密码才可以修改单元格内容 3 模板设置密码 确定原密码 设置新密码

    1 workbook.WriteProtectWorkbook("password", "admin"); 还是可以进去 只读进去 可以编辑 编辑就另存为   ...

  3. Intel processor brand names-Xeon,Core,Pentium,Celeron----Pentium

    http://en.wikipedia.org/wiki/Pentium Pentium From Wikipedia, the free encyclopedia     This article ...

  4. python处理blog文件数据

    以下是Python数据处理的题目说明与要求: The attachment is a log file used to show running status of set-top-box, and ...

  5. android-auto-scroll-view-pager

    https://github.com/eltld/android-auto-scroll-view-pager

  6. 2016/05/25 PHP mysql_insert_id() 函数 返回上一步 INSERT 操作产生的 ID

    定义和用法 mysql_insert_id() 函数返回上一步 INSERT 操作产生的 ID. 如果上一查询没有产生 AUTO_INCREMENT 的 ID,则 mysql_insert_id() ...

  7. junit使用小结

    1.spring中使用 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=CDPlayerConfig.cla ...

  8. Spring Base

    1.在java开发领域,Spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...

  9. ubuntu 本地和服务器scp文件传输

    安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh username@192.168.0.1 ...

  10. Spark高级

    Spark源码分析: https://yq.aliyun.com/articles/28400?utm_campaign=wenzhang&utm_medium=article&utm ...