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

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

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

17条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 11:36

    You may find this fast snippet useful in not so much long time intervals:

        from datetime import datetime as dttm
        time_ago = dttm(2017, 3, 1, 1, 1, 1, 1348)
        delta = dttm.now() - time_ago
        days = delta.days # can be converted into years which complicates a bit…
        hours, minutes, seconds = map(int, delta.__format__('').split('.')[0].split(' ')[-1].split(':'))
    

    tested on Python v.3.8.6

提交回复
热议问题