需要两步

为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型

1.字符串与time类型的转换

>>> import time
>>> timestr = "time2009-12-14"
>>> t = time.strptime(timestr, "time%Y-%m-%d")
>>> print t
(2009, 12, 14, 0, 0, 0, 0, 348, -1)

>>> type(t)
<type 'time.struct_time'>
>>>

如代码所示使用strptime进行转换,第一个参数是要转换的字符串,第二个参数是字符串中时间的格式

与之对应的有函数strftime,是将time类型转换相应的字符串

下面是格式化符号汇总

  %a 星期几的简写 Weekday name, abbr.
  %A 星期几的全称 Weekday name, full
  %b 月分的简写 Month name, abbr.
  %B 月份的全称 Month name, full
  %c 标准的日期的时间串 Complete date and time representation
  %d 十进制表示的每月的第几天 Day of the month
  %H 24小时制的小时 Hour (24-hour clock)
  %I 12小时制的小时 Hour (12-hour clock)
  %j 十进制表示的每年的第几天 Day of the year
  %m 十进制表示的月份 Month number
  %M 十时制表示的分钟数 Minute number
  %S 十进制的秒数 Second number
  %U 第年的第几周,把星期日做为第一天(值从0到53)Week number (Sunday first weekday)
  %w 十进制表示的星期几(值从0到6,星期天为0)weekday number
  %W 每年的第几周,把星期一做为第一天(值从0到53) Week number (Monday first weekday)
  %x 标准的日期串 Complete date representation (e.g. 13/01/08)
  %X 标准的时间串 Complete time representation (e.g. 17:02:10)
  %y 不带世纪的十进制年份(值从0到99)Year number within century
  %Y 带世纪部分的十制年份 Year number
  %z,%Z 时区名称,如果不能得到时区名称则返回空字符。Name of time zone
  %% 百分号

2.time类型与datetime类型的转换

这一步比较简单,使用datetime函数,代码如下

>>> import datetime
>>> d = datetime.datetime(* t[:6])

>>> type(d)
<type 'datetime.datetime'>
>>>

python字符串转日期的更多相关文章

  1. Python 字符串转换为日期

    应用程序接受字符串格式的输入,但是你想将它们转换为datetime 对象以便在上面执行非字符串操作. 使用Python 的标准模块datetime 可以很容易的解决这个问题.比如: >>& ...

  2. Python字符串和日期相互转换的方法

    import time,datetime # 日期转换成字符串 print time.strftime("%Y-%m-%d %X", time.localtime()) #字符串转 ...

  3. python字符串和日期相互转换

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

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

  5. python生成随机日期字符串

    python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...

  6. Python之字符串转换为日期、结合时区的日期操作

    一.字符串转换为日期 方法一 s = '2019-01-20' print(datetime.strptime(s, '%Y-%m-%d')) # 2019-01-20 00:00:00 方法二 de ...

  7. python datetime 时间日期处理小结

    python datetime 时间日期处理小结 转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232. ...

  8. Python时间和日期学习

    #coding=utf-8 __author__ = 'Administrator' #日期和时间模块学习 """ Python程序能用很多方式处理日期和时间,转换日期格 ...

  9. python字符串内容替换的方法(转载)

    python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法.   ...

随机推荐

  1. windows支持applocker的版本

    Operating system requirements   The following table show the on which operating systems AppLocker fe ...

  2. PHP性能追踪及分析工具xhprof的安装与使用

    对于本地开发环境来说,进行性能分析xdebug是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能追踪及分析. 我们今天就简单介绍一下x ...

  3. linux知识复习1-dup dup2

    #include <sys/stat.h> #include <string.h> #include <fcntl.h> #include <stdio.h& ...

  4. 使用Tslib在触摸屏上显示汉字【转】

    转自:http://www.latelee.org/embedded-linux/use-tslib-to-display-chinese-character.html 终于到了在触摸屏上显示汉字了, ...

  5. arm处理器中a5 a8 a9,v6 v7,arm7 arm9 arm11都是依据什么来分类的【转】

    转自:http://blog.csdn.net/maochengtao/article/details/9951131ARM处理器发展这么多年,有很多架构,很多不同的内核 架构有armv1 v2 v3 ...

  6. 【bzoj2212&3702】二叉树

    线段树合并入门题. 分别计算左子树的逆序对,右子树的逆序对,合并的时候计算贡献. #include<bits/stdc++.h> #define N 8000005 using names ...

  7. 【bzoj4810】由乃的玉米田

    lxl丧心病狂-- 首先允许离线的区间询问一看就是莫队.那么我们看下怎么莫队? 不会. "由乃题多半是不可做的."于是我看了下题解--好吧果然是bitset 用bitset维护当前 ...

  8. 【python】正则表达式相关

    注意:Python3.X 的print要有括号, Python 2.x的不需要 放上学习时写的例子: import re m = re.match(r'(\w+) (\w+)(?P<sign&g ...

  9. docker从零开始网络(六)Macvlan

    使用Macvlan网络 某些应用程序,尤其是遗留应用程序或监视网络流量的应用程序,希望直接连接到物理网络.在这种情况下,您可以使用macvlan网络驱动程序为每个容器的虚拟网络接口分配MAC地址,使其 ...

  10. python的上下文管理(context)(1)

    本文转载自:http://blog.csdn.net/G_66_hero/article/details/53048540 什么是Python中的上下文管理器 怎么使用上下文管理器 如何创建自己的上下 ...