Formatted question description: https://leetcode.ca/all/1360.html

# 1360. Number of Days Between Two Dates (Easy)

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 as shown in the examples.

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


Constraints:

• The given dates are valid dates between the years 1971 and 2100.

## Solution 1.

// OJ: https://leetcode.com/problems/number-of-days-between-two-dates/

// Time: O(1)
// Space: O(1)
// Ref: https://leetcode.com/problems/number-of-days-between-two-dates/discuss/517605/Similar-to-day-of-the-year
class Solution {
const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool isLeap(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
int daysFrom1971(string date) {
int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8));
for (int yr = 1971; yr < y; ++yr) d += isLeap(yr) ? 366 : 365;
return d + (m > 2 && isLeap(y)) + accumulate(begin(days), begin(days) + m - 1, 0);
}
public:
int daysBetweenDates(string date1, string date2) {
return abs(daysFrom1971(date1) - daysFrom1971(date2));
}
};


## Solution 2. Magic Formula

When m=1 or m=2 (January or February), we let m=13 or m=14 and let y decreased by 1. Imagine it is 13th or 14th month of the last year. By doing that, we let the magical formula also work for those two months. (153 * m + 8) // 5 is just a carefully designed way to record the days of each month. More specifically, it is designed to record the difference of days between two months. Suppose we have March 1st and April 1st, (153 * 3 + 8) // 5 = 93 while (153 * 4 + 8) // 5 = 124, the difference is 31 which is the number of days in March. Suppose we have April 1st to May 1st, (153 * 4 + 8) // 5 = 124 and (153 * 5 + 8) // 5 = 154, the difference is now 30 which is the number of days in April. You can also check other months.

// OJ: https://leetcode.com/problems/number-of-days-between-two-dates/

// Time: O(1)
// Space: O(1)
// Ref: https://leetcode.com/problems/number-of-days-between-two-dates/discuss/517582/Python-Magical-Formula
class Solution {
int days(string date) {
int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8));
if (m < 3) {
m += 12;
y -= 1;
}
return 365 * y + y / 4 + y / 400 - y / 100 + d + (153 * m + 8) / 5;
}
public:
int daysBetweenDates(string date1, string date2) {
return abs(days(date1) - days(date2));
}
};


Java

class Solution {
public int daysBetweenDates(String date1, String date2) {
String[] array1 = date1.split("-");
String[] array2 = date2.split("-");
int year1 = Integer.parseInt(array1[0]), month1 = Integer.parseInt(array1[1]), day1 = Integer.parseInt(array1[2]);
int year2 = Integer.parseInt(array2[0]), month2 = Integer.parseInt(array2[1]), day2 = Integer.parseInt(array2[2]);
if (year1 > year2 || year1 == year2 && month1 > month2 || year1 == year2 && month1 == month2 && day1 > day2) {
int temp = year1;
year1 = year2;
year2 = temp;
temp = month1;
month1 = month2;
month2 = temp;
temp = day1;
day1 = day2;
day2 = temp;
}
int numberOfDays1 = numberOfDays(year1, month1, day1);
int numberOfDays2 = numberOfDays(year2, month2, day2);
if (year1 == year2)
return numberOfDays2 - numberOfDays1;
else {
int totalDays = (year2 - year1) * 365 - numberOfDays1 + numberOfDays2;
int checkLeapYear = (int) Math.ceil(year1 / 4.0) * 4;
while (checkLeapYear < year2) {
if (isLeap(checkLeapYear))
totalDays++;
checkLeapYear += 4;
}
}
}

public int numberOfDays(int year, int month, int day) {
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeap(year))
monthDays[1]++;
int numberOfDays = 0;
for (int i = month - 2; i >= 0; i--)
numberOfDays += monthDays[i];
numberOfDays += day;
return numberOfDays;
}

public boolean isLeap(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
}