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. hdu 4985(模拟)

    Little Pony and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. Codeforces Gym100814 B.Unlucky Teacher (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)

    今日份的训练题解,今天写出来的题没有昨天多,可能是因为有些事吧... 这个题就是老师改卷子,忘带标准答案了,但是他改了一部分卷子,并且确定自己改的卷子没出错,他想从改过的卷子里把标准答案推出来. 因为 ...

  3. Implement Trie (Prefix Tree) - LeetCode

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 洛谷 P3359 改造异或树

    题目描述 给定一棵n 个点的树,每条边上都有一个权值.现在按顺序删掉所有的n-1条边,每删掉一条边询问当前有多少条路径满足路径上所有边权值异或和为0. 输入输出格式 输入格式: 第一行一个整数n. 接 ...

  5. IM即时通讯群组头像拼接.net core 解决方案

    一.需求概述 多人聊天(群组,讨论组,聊天室,以下统称: “群组” )生成一个拼接头像,需要把最先加入群组的几个人(最多4个人,以下简称:头部用户,A.B.C.D)的头像拼凑成在一起. 群组创建后,A ...

  6. ios iPhone 如何将应用程序名称本地化

    iPhone的应用程序名称也可以本地化,可以按照以下步骤来实施: 1. 修改项目目录下的’ -info.plist’文件名 将’ -info.plist’ 修改为 Info.plist 2. 将Inf ...

  7. Using CLEAR_BLOCK To Prevent Save Confirmation Dialogs In Oracle Forms

    Clear_Block built-in clears all records from the current data block and if the user had made some ch ...

  8. 修改Centos默认源

    原文:http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0.d6ykiD 1.备份 mv /etc/yum.repos.d/Cen ...

  9. make -j 4 echo !$

    make -j 4 #以cpu四核编译 !$上一次命令空格后的部分

  10. 微信小程序 - 单个题目

    后端传过来的数据,如果通过wx:for遍历出来那就是一个页面全部排下来.... 我的想法就是,页面初始化时设置一个默认值 1/50 就是 index+1 / 50(后端传过来的数组长度),通过控制in ...