• python3 获取两个时间戳相差多少天


    code

    import time
    import datetime
    
    t=datetime.datetime.now()
    
    #当前日期
    t1 =t.strftime('%Y-%m-%d %H:%M:%S')
    #转为秒级时间戳
    ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))
    #转为毫秒级
    end_time=int(str(ts1*1000).split(".")[0])
    
    
    #48小时前
    t2=(t-datetime.timedelta(hours=48)).strftime("%Y-%m-%d %H:%M:%S")
    #转为秒级时间戳
    ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))
    #转为毫秒级
    start_time=int(str(ts2*1000).split(".")[0])
    
    print("
    ","*"*30)
    
    print(start_time)
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))
    
    print("*"*30)
    
    print(end_time)
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))
    
    print("*"*30,"
    ")
    
    print("相差",(datetime.datetime.fromtimestamp(ts1)-datetime.datetime.fromtimestamp(ts2)).days,"")

    outputs

    macname@MacdeMBP Desktop % python3 test.py
    
     ******************************
    1588754711000
    2020-05-06 16:45:11
    ******************************
    1588927511000
    2020-05-08 16:45:11
    ****************************** 
    
    相差 2 天
    macname@MacdeMBP Desktop % 

     第二版本

    import time
    import datetime
    
    t=datetime.datetime.now()
    
    #当前时间
    t1 =t.strftime('%Y-%m-%d %H:%M:%S')
    #转为秒级时间戳
    second_timestamp1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))
    #转为毫秒级
    microsecond_timestamp1=int(str(second_timestamp1*1000).split(".")[0])
    
    #48小时前
    t2=(t-datetime.timedelta(hours=48)).strftime("%Y-%m-%d %H:%M:%S")
    #转为秒级时间戳
    second_timestamp2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))
    #转为毫秒级
    microsecond_timestamp2=int(str(second_timestamp2*1000).split(".")[0])
    
    print("
    ","*"*30)
    
    print("second_timestamp1:",second_timestamp1)
    print("microsecond_timestamp1:",microsecond_timestamp1)
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(second_timestamp1)))
    
    print("*"*30)
    
    print("second_timestamp2:",second_timestamp2)
    print("microsecond_timestamp2:",microsecond_timestamp2)
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(second_timestamp2)))
    
    print("*"*30,"
    ")
    
    print("相差",(datetime.datetime.fromtimestamp(second_timestamp1)-datetime.datetime.fromtimestamp(second_timestamp2)).days,"")

    outputs

    macname@MacdeMBP Desktop % python3 test.py
    
     ******************************
    second_timestamp1: 1588928019.0
    microsecond_timestamp1: 1588928019000
    2020-05-08 16:53:39
    ******************************
    second_timestamp2: 1588755219.0
    microsecond_timestamp2: 1588755219000
    2020-05-06 16:53:39
    ****************************** 
    
    相差 2 天
    macname@MacdeMBP Desktop % 

  • 相关阅读:
    -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.chan
    failed to export application
    IOS InHouse 发布流程
    BoneCP学习笔记
    form表单, css1
    HTTP协议, HTML
    自定义ORM框架
    数据库5
    数据库4
    数据库3
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12851450.html
Copyright © 2020-2023  润新知