s1='20120125';

  6     s2='20120216';
  7     a=time.strptime(s1,'%Y%m%d');
  8     b=time.strptime(s2,'%Y%m%d');
  9     a_datetime=datetime.datetime(*a[:3]);
 10     b_datetime=datetime.datetime(*b[:3]);
 11     print b_datetime-a_datetime;

http://blog.csdn.net/nankaihunter/article/details/5003327

为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成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])
>>> print d
2009-12-14 00:00:00

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

【python】time 和datetime类型转换,字符串型变量转成日期型变量的更多相关文章

  1. sql 将8位字符串转换成日期型

    将8位字符串转换成日期型,方法如下: ),)

  2. C# 中字符串转换成日期

    我们在处理字符串日期格式常用DateTime.Pares() 但是这个形式的转换是相当有限的,有些C#是会不懂你写入的日期格式的如20031231.那么类似 "20100101" ...

  3. 第二章 Python基本元素:数字、字符串和变量

    Python有哪些内置的数据类型: True False #布尔型 42 100000000 #整型 3.14159 1.0e8 #浮点型 abcdes #字符串 2.1 变量.名字和对象 pytho ...

  4. 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语

    数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...

  5. Python进阶:如何将字符串常量转化为变量?

    前几天,我们Python猫交流学习群 里的 M 同学提了个问题.这个问题挺有意思,经初次讨论,我们认为它无解. 然而,我认为它很有价值,应该继续思考怎么解决,所以就在私密的知识星球上记录了下来. 万万 ...

  6. Python datetime 格式化字符串:strftime()

    Python datetime 格式化字符串:strftime()   Python 的datetime模块 其实就是date和time 模块的结合, 常见的属性方法都比较常用 比如: datetim ...

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

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

  8. 『无为则无心』Python基础 — 11、Python中的数据类型转换

    目录 1.为什么要进行数据类型转换 2.数据类型转换本质 3.数据类型转换用到的函数 4.常用数据类型转换的函数 (1)int()函数 (2)float()函数 (3)str()函数 (4)bool( ...

  9. python模块time&datetime&json & picle&14.logging等

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

随机推荐

  1. poj_1306_Combinations

    Computing the exact number of ways that N things can be taken M at a time can be a great challenge w ...

  2. ABAP术语-Function Group

    Function Group 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/13/1067699.html Group of logical ...

  3. 查找mysql中未提交的事务

    1.查找未提交事务 在mysql中运行: select t.trx_mysql_thread_id from information_schema.innodb_trx t 2.删除线程 kill   ...

  4. js分割字符串

    js分割字符串 我想达到通过 : 分割 只要第一次分割,后面的内容不使用分割 不行,没找到可以直接用的方法,不过可以通过其它方式达到效果 eg: str.split(':',2)[0] (第一个分隔符 ...

  5. Centos在虚拟机VMware12上的安装

    欢迎评论和更正 虚拟机12的安装 看教程https://blog.csdn.net/yhj19920417/article/details/72891766 centos6.5镜像下载(选minima ...

  6. H5混合开发进阶

    混合开发: 原生app里面,IOS 安卓的原生app里面,嵌套h5界面. 通过原生app里的一个webView盒子进行交互.webView是原生app内置的一个XXX,里面可以放置h5界面.可以相互调 ...

  7. less学习三---父选择器

    引用父选择器需要用到“&”符号 &运算符表示嵌套规则的父选择器,并且在修改类或伪类选择器的应用中非常普遍 ul{ li{ &:nth-child(2) a { color: r ...

  8. [转]ThinkPHP5 隐藏index.php问题

    ThinkPHP5 隐藏index.php问题 Apache,修改.htaccess文件 ----------------------------------------------------- R ...

  9. Promise 的基础用法

    Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案–回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了语法,原生提供了Promi ...

  10. python循环,函数

    平常的网页会转换成ascll码,在转编译成十六进制a='http://www.mywebsit.com/?query=python&count=20'print(a)b=a.replace(' ...