'How to calculate number of days between two given dates in python? [duplicate]

I have two datetime objects <class 'datetime.datetime'>

d1 = 2022-04-25 08:27:36
d2 = 2022-01-28 20:41:57

What is the best way to get the number of days between these two dates in python?



Solution 1:[1]

You can simply use this function(if you're using YY-MM-DD):

def numOfDays(date1, date2):
    return (date2-date1).days

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ilai K