C++计算两个日期之间的天数的算法

180it 2020-10-13 PM 2107℃ 0条
#include <iostream>
#include <vector>
#include <string>
#include<sstream>

using namespace std;


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;
        }
    }
};




int main() {
   string date1 = "2020-01-01", date2 = "2020-12-31";
    Solution ga;
    std::cout<<ga.daysBetweenDates(date1,date2);

    return 0;
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++计算两个日期之间的天数的算法