Write a program to count the number of days between two dates.

The two dates are given as strings, their format is YYYY-MM-DD&nbsp;as shown in the examples.

&nbsp;
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15

&nbsp;
Constraints:


	The given dates are valid&nbsp;dates between the years 1971 and 2100.


from datetime import date

class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        return abs((date.fromisoformat(date2) - date.fromisoformat(date1)).days)
class Solution {
    public int daysBetweenDates(String date1, String date2) {
        String[] d1 = date1.split("-");
        String[] d2 = date2.split("-");
        return (int)Math.abs(
            daysFrom1971(Integer.parseInt(d1[0]), Integer.parseInt(d1[1]), Integer.parseInt(d1[2]))
            - daysFrom1971(Integer.parseInt(d2[0]), Integer.parseInt(d2[1]), Integer.parseInt(d2[2])));
    }
    private int daysFrom1971(int year, int month, int day) {
        int total = 0;
		// count years first
        total += (year - 1971) * 365;
        for (int i = 1972; i < year; i += 4) {
            if (isLeapYear(i)) total++;
        }        
        int feb = isLeapYear(year) ? 29 : 28;
		// sum months and days
        switch (month) {
            case 12: 
                total += 30; // 11
            case 11:
                total += 31; // 10
            case 10: 
                total += 30; // 9
            case 9:
                total += 31; // 8
            case 8:
                total += 31; // 7
            case 7: 
                total += 30; // 6
            case 6:
                total += 31; // 5
            case 5:
                total += 30; // 4
            case 4: 
                total += 31; // 3
            case 3: 
                total += feb; // 2
            case 2:
                total += 31;
            case 1:
                total += day;                
        }
        return total;
    }
    private boolean isLeapYear(int i) {
        return (i % 4 == 0) && ((i % 100 == 0 && i % 400 == 0) || i % 100 != 0);
    }
}
class Solution
{
public:
    int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    bool isLeap(int y)
    {
        return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
    }
    int calc(string s)
    {
        int y = stoi(s.substr(0, 4));
        int m = stoi(s.substr(5, 2));
        int d = stoi(s.substr(8));
        for (int i = 1971; i < y; i++)
            d += isLeap(i) ? 366 : 365;
        d += accumulate(begin(days), begin(days) + m - 1, 0);
        d += (m > 2 && isLeap(y)) ? 1 : 0;
        return d;
    }
    int daysBetweenDates(string date1, string date2)
    {
        int ans = abs(calc(date2) - calc(date1));
        return ans;
    }
};
var daysBetweenDates = function(date1, date2) {
     let miliSecondInaDay = 24*60*60*1000;
     if(date1>date2) return (new Date(date1) - new Date(date2)) / miliSecondInaDay
     else return (new Date(date2) - new Date(date1)) / miliSecondInaDay
};
Number of Days Between Two Dates