Algorithm to Compute the Number of Days Between Two Dates

  • 时间:2020-09-10 13:27:27
  • 分类:网络文摘
  • 阅读:140 次

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.

Hints:
Create a function f(date) that counts the number of days from 1900-01-01 to date. How can we calculate the answer ?
The answer is just |f(date1) – f(date2)|.
How to construct f(date) ?
For each year from 1900 to year – 1 sum up 365 or 366 in case of leap years. Then sum up for each month the number of days, consider the case when the current year is leap, finally sum up the days.

How to Compute the Number of Days Between Two Dates?

First, we have to extract the integer values for the Year, Month and Day between two date strings. Then, we need a function e.g. f(d) to tell us the number of days since a fixed Date e.g. 1900-01-01. Then, the difference between two dates can be simplify computed by the absolute difference i.e. |f(d1) – f(d2)|.

We need the isLeap function to tell us if a given year is leap. A leap year has 366 days instead of a regular 365 days. Also, in a leap year, the Feb month has 29 days instead of 28 days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
    int daysBetweenDates(string date1, string date2) {
        return abs(getDays(date1) - getDays(date2));
    }
    
private:
    int getDays(string date) {
        int year = atoi(date.substr(0, 4).c_str());
        int month = atoi(date.substr(5, 2).c_str());
        int day = atoi(date.substr(8, 2).c_str());
        int ans = 0;
        for (int i = 1900; i < year; ++ i) {
            if (isLeap(i)) {
                ans += 366;
            } else {
                ans += 365;
            }
        }
        for (int i = 1; i < month; ++ i) {
            switch(i) {
                case 1: ans += 31; break;
                case 2: ans += isLeap(year) ? 29 : 28; break;
                case 3: ans += 31; break;
                case 4: ans += 30; break;
                case 5: ans += 31; break;
                case 6: ans += 30; break;
                case 7: ans += 31; break;
                case 8: ans += 31; break;
                case 9: ans += 30; break;
                case 10: ans += 31; break;
                case 11: ans += 30; break;
                case 12: ans += 31; break;
            }
        }
        return ans += day - 1;
    }
    
    bool isLeap(int Y) {
        if (Y % 400 == 0) {
            return true;
        } else if ( Y % 100 == 0) {
            return false;
        } else if (Y % 4 == 0) {
            return true;
        } else {
            return false;
        }        
    }
};
class Solution {
public:
    int daysBetweenDates(string date1, string date2) {
        return abs(getDays(date1) - getDays(date2));
    }
    
private:
    int getDays(string date) {
        int year = atoi(date.substr(0, 4).c_str());
        int month = atoi(date.substr(5, 2).c_str());
        int day = atoi(date.substr(8, 2).c_str());
        int ans = 0;
        for (int i = 1900; i < year; ++ i) {
            if (isLeap(i)) {
                ans += 366;
            } else {
                ans += 365;
            }
        }
        for (int i = 1; i < month; ++ i) {
            switch(i) {
                case 1: ans += 31; break;
                case 2: ans += isLeap(year) ? 29 : 28; break;
                case 3: ans += 31; break;
                case 4: ans += 30; break;
                case 5: ans += 31; break;
                case 6: ans += 30; break;
                case 7: ans += 31; break;
                case 8: ans += 31; break;
                case 9: ans += 30; break;
                case 10: ans += 31; break;
                case 11: ans += 30; break;
                case 12: ans += 31; break;
            }
        }
        return ans += day - 1;
    }
    
    bool isLeap(int Y) {
        if (Y % 400 == 0) {
            return true;
        } else if ( Y % 100 == 0) {
            return false;
        } else if (Y % 4 == 0) {
            return true;
        } else {
            return false;
        }        
    }
};

The above C++ code to compute the number of days between two dates uses the atoi function to convert the string (the .c_str() gives char*) to integer.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
保健酒乱象调查:为见效快违法添加伟哥  饮用蜂蜜水的最佳时间及注意事项  红枣维生素含量高 喝大枣水养肝排毒  黑木耳营养丰富对健康有五大好处  香蕉和橘子能起到解毒护肝的作用  吃葡萄、喝葡萄酒能帮助调节性功能  绿茶、红茶、青茶、黑茶、白茶和黄茶  奶茶多添加奶精 长期食用会引发心脏病  奶茶调查:街头奶茶店调香味多用奶精  适合秋天食用的养肺食谱可滋阴润肺 
评论列表
添加评论