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 时间格式化(转)的更多相关文章

  1. python时间格式化

    import timeprint time.time()输出的结果是:1279578704.6725271 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来处理: ...

  2. python 时间格式化

    2018-10-25 17:49:51 监控软件 原始 2018-10-2517:49:51 切割后 2018/10/25 17:49:51 格式化后 shijian = ' '.join(respo ...

  3. python时间格式化、运行时长计算

    1.格式化: import time timeStr=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #格式化为:2018-07-17 19:0 ...

  4. python时间格式化及前推

    import datetime >>> (datetime.datetime.now() - datetime.timedelta(seconds = 300)).strftime( ...

  5. java和python的时间格式化区别

    java 和 python时间格式化区别 月份,java是M,python是m 分钟,java是m,python是M 年份,必须用yyyy,表示当天所在的年份,如果用YYYY,则表示当前周所在年份 j ...

  6. Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化

    一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...

  7. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

  8. Python 的时间格式化

    对于像'Wed, 11 Apr 2012 09:37:05 +0800'的时间格式化可如下解: >>> date='Wed, 11 Apr 2012 09:37:05 +0800'& ...

  9. python 时间字符串与日期转化

    python 时间字符串与日期转化 datetime.datetime.strptime(string, format) 根据指定的格式解析字符串为一个datetime类型.相当于datetime.d ...

随机推荐

  1. 剖析CPU温度监控技术【转】

    转自:http://blog.csdn.net/hunanchenxingyu/article/details/46476545 迄今为止还没有一种cpu散热系统能保证永不失效.失去了散热系统保护伞的 ...

  2. linux mmap 详解【转】

    转自:http://blog.chinaunix.net/uid-20321537-id-3483405.html 一.前言mmap的具体实现以前在学习内核时学习过,但是对于其中的很多函数是一知半解的 ...

  3. C# List排序,附加使用Linq排序

    首先先介绍一下平时最常用的几种排序方法. 第一种:实体类实现IComparable接口,而且必须实现CompareTo方法 实体类定义如下: class Info:IComparable { publ ...

  4. c#随机生成汉字、字母、数字

    /// <summary> /// 替换变量 /// </summary> /// <param name="content"></par ...

  5. 在C#用进程打开cmd与直接手动打开cmd是不一样的

    网上找不着答案,想了一下觉得可能是不同的用户,用set看了一下环境变量果然是不同用户,要改一下 按以下方法改: http://www.cnblogs.com/babycool/p/3569183.ht ...

  6. loj #100. 矩阵乘法

    题目描述 这是一道模板题. 分别给定 n×p n \times pn×p 和 p×m p \times mp×m 的两个矩阵 A AA 和 B BB,求 A×B A \times BA×B. 输入格式 ...

  7. centos7安装hadoop2.6.1,详细教程

    1.我搭建的是三台centos7的环境 首先准备三个centos7(文中出现的所有的链接都是我自己的) centos7下载地址(也可以上官网自行下载):https://pan.baidu.com/s/ ...

  8. 模型搭建练习1_用numpy和tensor、variable实现前后向传播、实现激活函数

    用numpy实现搭建一个简单的forward和backward import numpy as np N, D_in, H, D_out = 64, 1000, 100, 10 x = np.rand ...

  9. gitlab gitlab runner

    1.安装gitlab https://about.gitlab.com/installation/#ubuntu 2.安装runner https://docs.gitlab.com/runner/i ...

  10. 深入理解 JavaScript Function

    1.Function Arguments JavaScript 函数的参数 类型可以是 复杂类型如  Object or Array 和简单类型 String Integer null undefin ...