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)
  • 相关阅读:
    用ASP+DLL实现WEB方式修改服务器时间
    参加了 湖南.NET俱乐部成立大会
    Asp.Net中文本换行
    一直在思考的问题
    GRIDVIEW排序 动态实现和静态实现
    在VS 2005中使用TREEVIEW控件
    GRIDVIEW 中当数据行数未满时,填充空白行
    为了自己的心身健康 合理安排生活 特做了张时间安排表
    在VS 2005后台代码中创建用户控件
    CSS IE7 IE6 Firefox多浏览器兼容(转&摘)
  • 原文地址:https://www.cnblogs.com/xdlzs/p/10729954.html
Copyright © 2011-2022 走看看