前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python获取指定时间差的时间

python获取指定时间差的时间

作者头像
IT架构圈
发布2018-05-31 15:59:49
2K0
发布2018-05-31 15:59:49
举报
文章被收录于专栏:IT架构圈IT架构圈

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

代码语言:javascript
复制
    '''
    Created on 2018年02月3日
    '''
    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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档