python获取指定时间差的时间
2016-05-03 20:48
656 查看
在分析数据的时间经常需要截取一定范围时间的数据,比如三天之内,两小时前等等时间要求的数据,因此将该部分经常需要用到的功能模块化,方便以后以后用到的时候复用。在此,也分享给大家。
运行后所得结果:
个人博客新址 : dataminingclub
<span style="font-size:18px;">#coding:utf-8 ''' Created on 2016年5月3日 @author: baocheng ''' import time import sys reload(sys) def get_day_of_day(UTC=False, days=0, hours=0, miutes=0, seconds=0): ''''' if days>=0,date is larger than today if days<0,date is less than today date format = "YYYY-MM-DD" ''' now = time.time() timeNew = now + days*24*60*60 + hours*60*60 + miutes*60 + seconds if UTC : timeNew = timeNew + time.timezone t = time.localtime(timeNew) return time.strftime('%Y-%m-%d %H:%M:%S', t) #使用UTC时间 两小时前 t = get_day_of_day(True,0,-2) print t #当地时间 三天前 t = get_day_of_day(False,-3) print t #当地时间 三天后 t = get_day_of_day(False,3) print t</span>
运行后所得结果:
<span style="font-size:18px;">2016-05-03 10:25:56 2016-04-30 20:25:56 2016-05-06 20:25:56</span>
个人博客新址 : dataminingclub
相关文章推荐
- python获取指定时间差的时间
- python 中文乱码问题
- Python json模块
- python 图片格式转换产生的锯齿问题
- python入门,自制小爬虫获得百度贴吧内容
- Python为什么这么火?
- 步步学习python之环境安装:Vagrant+Ubuntu+Emacs+Virtualenv+Python3开发环境的搭建(Mac电脑下)3
- ubuntu14中创建python虚拟环境
- 使用spyder编译器单步调试python
- 234. Palindrome Linked List [easy] (Python)
- python 模块
- Python pass 语句
- Python中函数参数设置及使用的学习笔记
- python 汉诺塔 Fibonacci数列
- 由PYTHONPATH谈起
- python 安装django
- Python制作统计图形
- Python基于pandas的数据处理(二)
- web框架python
- Eclipse + Pydev开发Python时import报错解决方法