Search code examples
pythondatetimeunixunix-timestamp

How to calculate UNIX timestamp difference using python?


I want the diff between two timestamps which are available to me as list of strings.

date_list = ['Tue Sep 25 21:12:32 PDT 2018', 'Tue Sep 25 21:15:27 PDT 2018']

How do I get the difference in minutes or in hours ?

I saw some examples, but they are using different format of UNIX timestamp. I can't change the given date format.


Solution

  • I suggest to use dateutil. For instance

    from dateutil import parser
    
    d1 = parser.parse(date_list[0])
    d2 = parser.parse(date_list[1])
    

    at this point d1 and d2 are standard datetime objects and you can use the standard methods/operators to operate on them