python获取指定时间差的时间实例详解

yipeiwu_com6年前Python基础

python获取指定时间差的时间实例详解

在分析数据的时间经常需要截取一定范围时间的数据,比如三天之内,两小时前等等时间要求的数据,因此将该部分经常需要用到的功能模块化,方便以后以后用到的时候复用。在此,也分享给大家。

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

运行后所得结果:

2016-04-30 20:25:56 
2016-05-06 20:25:56

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

python基于mysql实现的简单队列以及跨进程锁实例详解

通常在我们进行多进程应用开发的过程中,不可避免的会遇到多个进程访问同一个资源(临界资源)的状况,这时候必须通过加一个全局性的锁,来实现资源的同步访问(即:同一时间里只能有一个进程访问资源...

python3.6.3安装图文教程 TensorFlow安装配置方法

python3.6.3安装图文教程 TensorFlow安装配置方法

本文主要介绍Python3.6及TensorFlow的安装和配置流程。 一、Python官网下载自己电脑和系统对应的Python安装包。  网址:下载地址 一直往下拉到Fil...

Python中for循环控制语句用法实例

Python中for循环控制语句用法实例

本文实例讲述了Python中for循环控制语句用法。分享给大家供大家参考。具体分析如下: 第一个:求 50 - 100 之间的质数 import math for i in ran...

Python使用minidom读写xml的方法

本文实例讲述了Python使用minidom读写xml的方法。分享给大家供大家参考。具体分析如下: 一 python提供的xml支持 2种工业标准的xml解析方法-SAX和DOM。SAX...

python3实现绘制二维点图

python3实现绘制二维点图

如下所示: import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器,,,...