zoukankan      html  css  js  c++  java
  • Python time模块

    import time

    #1、时间戳 从unix元年到现在过了多少秒
    #2、格式化好的时间

    #先转成时间元组

    # print(time.time()) #获取当前时间戳
    # time.sleep(10)
    today = time.strftime('%Y%m%d%H%M%S')
    # print(today)

    # print(time.gmtime()) #默认取的是标准时区的时间
    s=time.localtime(1514198608) #取到的是当前时区的时间
    # print(time.strftime('%Y-%m-%d %H:%M:%S',s))
    #时间戳转换时间元组
    # 1、时间戳转成时间元组 time.localtime()
    # 2、再把时间元组转成格式化的时间
    def timestamp_to_fomat(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
    #1、默认返回当前格式化好的时间
    #2、传入时间戳的话,把时间戳转换成格式化好的时间,返回
    if timestamp:
    time_tuple = time.localtime(timestamp)
    res = time.strftime(format,time_tuple)
    else:
    res = time.strftime(format) #默认取当前时间
    return res

    # 2018-4-21
    # tp = time.strptime('2018-4-21','%Y-%m-%d') #把格式化好的时间转成时间元组的
    # print(time.mktime(tp)) #把时间元组转成时间戳
    def strToTimestamp(str=None,format='%Y%m%d%H%M%S'):
    # 20180421165643
    #默认返回当前时间戳
    if str: #如果传了时间的话
    tp = time.strptime(str,format) #格式化好的时间,转成时间元组
    res = time.mktime(tp)#再转成时间戳
    else:
    res = time.time() #默认取当前的时间戳
    return int(res)

    import datetime
    print(datetime.datetime.today()) #获取当前时间,精确到秒
    print(datetime.date.today()) #精确到天
    res = datetime.datetime.today()+datetime.timedelta(days=1,minutes=5,seconds=5,weeks=5)
    print(res.strftime('%Y-%m-%d'))

  • 相关阅读:
    loj 1257 (求树上每一个点到树上另一个点的最长距离)
    loj 1032 数位dp
    loj 1030概率dp
    loj1011 状态压缩
    java大数取模
    求阶乘的位数
    loj 1426(dfs + bfs)
    携程greenlet模块使用
    如何让socket编程非阻塞?
    分别用request和socket给百多发送请求
  • 原文地址:https://www.cnblogs.com/irisx/p/8907803.html
Copyright © 2011-2022 走看看