Python 时间格式化(转)
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'
Python 时间格式化(转)的更多相关文章
- python时间格式化
import timeprint time.time()输出的结果是:1279578704.6725271 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来处理: ...
- python 时间格式化
2018-10-25 17:49:51 监控软件 原始 2018-10-2517:49:51 切割后 2018/10/25 17:49:51 格式化后 shijian = ' '.join(respo ...
- python时间格式化、运行时长计算
1.格式化: import time timeStr=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #格式化为:2018-07-17 19:0 ...
- python时间格式化及前推
import datetime >>> (datetime.datetime.now() - datetime.timedelta(seconds = 300)).strftime( ...
- java和python的时间格式化区别
java 和 python时间格式化区别 月份,java是M,python是m 分钟,java是m,python是M 年份,必须用yyyy,表示当天所在的年份,如果用YYYY,则表示当前周所在年份 j ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- Python中日期和时间格式化输出的方法
本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...
- Python 的时间格式化
对于像'Wed, 11 Apr 2012 09:37:05 +0800'的时间格式化可如下解: >>> date='Wed, 11 Apr 2012 09:37:05 +0800'& ...
- python 时间字符串与日期转化
python 时间字符串与日期转化 datetime.datetime.strptime(string, format) 根据指定的格式解析字符串为一个datetime类型.相当于datetime.d ...
随机推荐
- android 的渐变背景设置文件
main.xml:<button android:layout_width="wrap_content" android:layout_height="wrap_c ...
- AC日记——[中山市选2009]谁能赢呢? bzoj 2463
2463 思路: 博弈: 把先手和后手的走的两个格子看做一个1*2的方格: 如果n为偶数,那么棋盘一定可以被1*2的方格覆盖: 前端为先手,后端为后手: 那么,当还剩下一个1*2的方格时,先手一定可以 ...
- Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)
补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...
- Oracle 索引(转)
一.索引介绍 1.1 索引的创建语法: CREATE UNIUQE | BITMAP INDEX <schema>.<index_name> ON <schema> ...
- java获取整数的各位数值
第一种是取模运算 int qian =input/1000; //千位除以1000 int bai = input/100%10;//百位除以100%10 int shi = input%100/10 ...
- MyBatis一级缓存(转载)
<深入理解mybatis原理> MyBatis的一级缓存实现详解 及使用注意事项 http://demo.netfoucs.com/luanlouis/article/details/41 ...
- codevs——1576 最长严格上升子序列(序列DP)
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给一个数组a1, a2 ... an,找到最长的上升降子序列 ...
- CodeForces - 361D Levko and Array
Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...
- Aria2使用教程
Aria2 是一个轻量级多协议和多源命令行下载实用工具.它支持 HTTP / HTTPS, FTP, SFTP, bt 和 Metalink.通过内置 Aria2 可以操作 json - rpc 和 ...
- 标签页的切换方法(DOM)
效果: 1.点击“JAVA语言” 2.点击“C语言” 3.点击C++语言 代码: <!doctype html> <html> <head> <meta ch ...