python-tutorialsmade

Calculate hours, minutes between two times in Python

I’m basically a PHP developer, but there was a situation where I have to work with a Python script, you know what? I have to find hours between two times, but that was not so easy for me. Even I searched in Google a lot, I couldn’t find an easy method to calculate hours between two times in Python.

Finally, after a long time of research I got some code which helped to find days between two dates, then I sat for sometime and wrote a script which gives hours minutes and seconds between two dates.

Here is the script:

Read the comments in the program to understand it.

Enjoy the day.

Related Posts

One thought on “Calculate hours, minutes between two times in Python

  1. >>> import datetime
    >>>
    >>> d1 = datetime.datetime(2014,11,1,23,59,59)
    >>> d2 = datetime.datetime(2014,11,10,20,0,9)
    >>>
    >>> (d2-d1).total_seconds()/60/60 # hours
    212.00277777777777
    >>>

    😀

Leave a Reply