Search code examples
pythondatetimeunix-timestamptimedelta

How to get Time offset (diffrence) b/w time.strftime and datetime.datetime?


I am getting time input in unix (milliseconds) and want to get time difference b/w given value and current time for that I did following. 1st convert posix into human readable then tried to minus 2 values but - doesn't go well with str.

/1000 is for millisecond reduction

t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1496814300000/1000))
k = datetime.datetime.now()

j = k -t
print j

Solution

  • Try turning your timestamp into a datetime object directly:

    import datetime
    t = datetime.datetime.fromtimestamp(1496814300000/1000)
    k = datetime.datetime.now()
    
    j = k - t
    print(j)