python - Difference between two unix timestamps

前端 未结 2 1675
难免孤独
难免孤独 2020-12-17 22:57

I have two timestamps in miliseconds and i want to compute the difference between the two in minutes:

d1 = 1502053449617 

current_time_utc = int(round(time         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-17 23:42

    You don't need to format the string, you just need to convert the timestamp directly, by first dividing it by 1000. Then its just a matter of printing out the differences (and calculating it in minutes):

    from __future__ import division
    import datetime
    
    d1 = 1502053449617
    
    converted_d1 = datetime.datetime.fromtimestamp(round(d1 / 1000))
    current_time_utc = datetime.datetime.utcnow()
    
    print((current_time_utc - converted_d1))
    print((current_time_utc - converted_d1).total_seconds() / 60)
    

    The above prints:

    3 days, 5:08:14.087515
    4628.234791916667
    

提交回复
热议问题