Search code examples
pythonpython-3.xtimeepoch

Subtracting the UNIX Epoch time from the current time in Python


I have a list of UNIX epoch times (Python), something like this:

[1526496663206, 1526507363973, 1526507363973, 1526574425012]

I want to subtract them from the current time and get the left time, for example, suppose 1526496663206 represents 2018-10-03 04:53:26. I want to subtract it from the current time and get the left time in a readable format.

I can get the current time with int(time.time()).

Now how do I subtract the list of UNIX times from it? Simply doing int(time.time()) - list[i] doesn't seem to work.

How do I do that? Is there some Python function for doing that> Thanks in advance?


Solution

  • Try this:

    import time
    
    
    current_time = time.time()
    print(current_time)
    diff = current_time - 'YOUR EPOCH TIME'
    time_left = now - diff
    print(time_left)
    
    formatted_time = time.strftime('%Y-%m-%d %I:%M:%S %p', time.localtime(time_left))
    print(formatted_time)