python-整理--时间模块
2016-02-18 21:05
477 查看
使用datetime模块处理时间
########################################################################### # # datetime模块 # ########################################################################### # 导入datetime类,取了别名DateTime(和C#一样的) from datetime import datetime as DateTime # 当前时间,按2016-02-05 13:01:01的格式输出 print(DateTime.now().strftime('%Y-%m-%d %H:%M:%S')) # 当前日期,按2016年02月15格式输出 print(DateTime.now().date().strftime('%Y{y}%m{m}%d').format(y='年', m='月')) # 将字符串转为DateTime.第二个参数是字符串的格式,这个格式要匹配进行转换的字符串 date='2016/02/23' print(type(DateTime.strptime(date,'%Y/%m/%d'))) print(DateTime.strptime(date,'%Y/%m/%d')) ## datetime类的一些属性 # 最小日期 0001-01-01 00:00:00 print(DateTime.min) # 最大日期 9999-12-31 23:59:59.999999 print(DateTime.max) # 在比较时间时间隔时,能比较出的时间差别. 0:00:00.000001(1微秒) print(DateTime.resolution) ## datetime对象的一些属性 curr=DateTime.now() # 时间区域信息,没有就显示None print(curr.tzinfo) # 年2016 月2 日5 时1 分12 秒23 微秒123456(1000000) print(curr.year) print(curr.month) print(curr.day) print(curr.hour) print(curr.minute) print(curr.second) print(curr.microsecond) ## 时间的操作 # 时间差 t1=DateTime.strptime('2016-02-05 12:20:00','%Y-%m-%d %H:%M:%S') t2=DateTime.strptime('2016-02-06 12:00:00','%Y-%m-%d %H:%M:%S') # 时间相减的结果是datetime.timedelta类 diff=t1-t2 # <class 'datetime.timedelta'> print(type(diff)) # -1 day, 0:20:00 print(diff) # 相差天数 相差秒数 相差微秒数 print(diff.days) # -1 print(diff.seconds) # 1200 print(diff.microseconds) # 0 # 还有很多属性和方法在python3.4文档上.
相关文章推荐
- python中Matplotlib的坐标轴的坐标区间的设定
- [转载]python的常用代码模板
- python import 引入简单介绍
- Python应用matplotlib绘图简介
- python面试问题题目
- python简单爬虫
- Python显示股票直线图和K线图
- 10 个 Python IDE 和代码编辑器
- python 截取 取出一部分的字符串
- 关于python同一个文件夹里面不同.py文件的交叉引用
- python开发之——pip安装(windows环境下)
- Python获取Yahoo股票数据
- python -- subprocess
- python第三天
- Python安装Scrapy
- Python Paramiko模块安装和使用
- python int与str转换
- python 不定参数
- python 文本单词提取和词频统计
- 使用Python简单的实现树莓派的WEB控制