Python 时间格式化(转)
2024-09-04 16:36:13
From:http://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html
http://www.wklken.me/posts/2015/03/03/python-base-datetime.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 ( - )
%H Hour in -hour format ( - )
%I Hour in -hour format ( - )
%j Day of year as decimal number ( - )
%m Month as decimal number ( - )
%M Minute as decimal number ( - )
%p Current locale's A.M./P.M. indicator for 12-hour clock
%S Second as decimal number ( - )
%U Week of year as decimal number, with Sunday as first day of week ( - )
%w Weekday as decimal number ( - ; Sunday is )
%W Week of year as decimal number, with Monday as first day of week ( - )
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number ( - )
%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
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'
最新文章
- 漫步ASP.NET MVC的处理管线
- react native AsyncStorage的使用
- I/O工作机制
- jquery插件-表单验证插件-demo
- 用DOS命令打开IE浏览器、我的文档等等
- iOS开发——UI基础-Xcode资源拷贝
- Spring 文章推荐
- Ubuntu软件中心打不开,Encountered a section with no Package: header错误之解决
- Dialog类介绍
- Python 入門語法和類型(转载学习)
- jquery easyui读取json文件乱码
- MyISAM读写并发优化
- 使用openxml读取xml数据
- [Locked] Meeting Room I &;&; II
- 使用lock_sga和pre_page_sga参数保证SGA常驻物理内存 .
- Android UI目录
- sql tran, c# SqlTransaction , TransactionScope 的用法
- 转 android客户端版本检测更新,服务下载,通知栏显示
- @class指令的使用
- div始终在底部