zoukankan      html  css  js  c++  java
  • 时间处理

    # timeStamp = 1381419600
    # timeArray = time.localtime(timeStamp)
    # otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    # print otherStyleTime # 2013--10--10 23:40:00
    # # 使用datetime
    # timeStamp = 1381419600
    # dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    # otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
    # print otherStyleTime # 2013--10--10 15:40:00



    # 时间戳转换为格式化
    import time
    timeStamp = 1570695095
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    print(otherStyleTime)
    # 格式化转换成时间戳
    import time
    def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):
    time_tuple = time.strptime(str_time, format) # 把格式化好的时间 转换成元祖
    result = time.mktime(time_tuple) # 把时间元祖转换成时间戳
    return int(result)
    print(str_to_timestamp('2019-09-21 00:00:00'))
     
    # 将时间转换成秒
    def t2s(t):
    h, m, s = t.strip().split(":")
    return int(h) * 3600 + int(m) * 60 + int(s)
    print(t2s('10:35:50'))
     
    获取昨天日期
    import datetime
    t = datetime.date.today()
    t2 = datetime.timedelta(-1)
    print(t)
    print(t2)
    print(t+t2)
     
    获取当前日期时间
    import datetime
    import time
    print(time.strftime('%Y%m%d %H:%M:%S',time.localtime(time.time())))
     
    获取当天凌晨
    import datetime
    import time
    today = datetime.date.today()
    print(today)
    today_time = int(time.mktime(today.timetuple()))
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(today_time)))
     
     
    import datetime
    # 获取当前时间
    now = datetime.datetime.now()
    # 获取今天零点
    zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,microseconds=now.microsecond)
    # 获取23:59:59
    lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
    print(lastToday)
  • 相关阅读:
    Android中View绘制流程以及invalidate()等相关方法分析
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    servlet上传文件报错(一)
    白话经典算法系列之六 高速排序 高速搞定
    POJ1177+线段树+扫描线
    tensorflow compile
    算法编程题的心得体会
    算法编程题的心得体会
    标识变量的使用
    标识变量的使用
  • 原文地址:https://www.cnblogs.com/xdlzs/p/10729954.html
Copyright © 2011-2022 走看看