How do I find the time difference between two datetime objects in python?

前端 未结 17 1348
無奈伤痛
無奈伤痛 2020-11-22 11:06

How do I tell the time difference in minutes between two datetime objects?

17条回答
  •  时光说笑
    2020-11-22 11:33

    Just subtract one from the other. You get a timedelta object with the difference.

    >>> import datetime
    >>> d1 = datetime.datetime.now()
    >>> d2 = datetime.datetime.now() # after a 5-second or so pause
    >>> d2 - d1
    datetime.timedelta(0, 5, 203000)
    

    You can convert dd.days, dd.seconds and dd.microseconds to minutes.

提交回复
热议问题