转自: 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'

最新文章

  1. head/tail实现
  2. Hadoop学习笔记—21.Hadoop2的改进内容简介
  3. NSString使用stringWithFormat拼接的相关知识
  4. Linux Dynamic Shared Library && LD Linker
  5. vue学习笔记之v-if
  6. STL中vector小结
  7. form表单那点事儿(上) 基础篇
  8. wp8 入门到精通 WebClient Post
  9. 04_IOC容器装配Bean(xml方式)
  10. 百度编辑器Ueditor自动换行,添加<p>的问题
  11. sql有几种删除表数据的方式
  12. UCI
  13. iOS开发之通知机制
  14. dom 事件主要内容
  15. root用户无法通过ssh连接Linux系统
  16. 3. tomcat 内存设置
  17. python面试笔试题,你都会了吗?快来复习
  18. kolla之docker私有仓库创建
  19. oracle函数,游标,视图使用总结
  20. JDBC存储过程调用

热门文章

  1. 鼠标滚轮实现图片的缩放-------Day79
  2. 【手势交互】4. Kinect for XBox
  3. String 字符串基本使用
  4. Centos6.5 安装 Oracle11gR2(64位)
  5. 【读后感】Netty 系列之 Netty 高性能之道 - 相比 Mina 怎样 ?
  6. 常用的sql命令
  7. iOS中UIPickerView常见属性和方法的总结
  8. hdu 1963 Investment 解题报告
  9. codeforces 454B. Little Pony and Sort by Shift 解题报告
  10. CentOS 7中ip命令将逐渐取代 ifconfig