服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现比较日期大小的示例代码

C++实现比较日期大小的示例代码

2023-04-06 18:58欧特克_Glodon C/C++

这篇文章主要为大家详细介绍了如何使用C++实现比较日期大小的功能,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的可以了解一下

一、目的

用来比较两个日期。日期格式:2023-03-31 09:16:56。

二、代码

?
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//std::wstring strA = L"2023-03-31 09:16:56";
//std::wstring strB = L"2023-03-31 09:21:34";
bool LessThanEx(std::wstring strA, std::wstring strB)
{
    std::wstring strLeftA, strRightA;
    std::wstring strLeftB, strRightB;
    {
        std::wstring strLeft, strRight;
        std::size_t nIndex = strA.find(L" ");
        if (nIndex!=std::string::npos)
        {
            strLeft = strA.substr(0,nIndex);
            strRight = strA.substr(nIndex+1);
 
            std::wstring wsDivide = L"-";
            strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
            strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
 
            wsDivide = L":";
            strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
            strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
        }
 
        strLeftA = strLeft;
        strRightA = strRight;
    }
 
    {
        std::wstring strLeft, strRight;
        std::size_t nIndex = strB.find(L" ");
        if (nIndex!=std::string::npos)
        {
            strLeft = strB.substr(0,nIndex);
            strRight = strB.substr(nIndex+1);
 
            std::wstring wsDivide = L"-";
            strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
            strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
 
            wsDivide = L":";
            strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
            strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
        }
 
        strLeftB = strLeft;
        strRightB = strRight;
    }
 
    __int64 nLeftA = std::stoi(strLeftA);
    __int64 nLeftB = std::stoi(strLeftB);
 
    __int64 nRightA = std::stoi(strRightA);
    __int64 nRightB = std::stoi(strRightB);
    if(nLeftA < nLeftB)
    {
        return true;
    }
    else if(nLeftA > nLeftB)
    {
        return false;
    }
    else
    {
        if(nRightA >= nRightB)
        {
            return false;
        }
        
        return true;
    }
 
    return true;
}
 
//CString strA = _T("2023-03-31 09:16:56");
//CString strB = _T("2023-03-31 09:21:34");
bool LessThan(CString strA, CString strB)
{
    CString strLeftA, strRightA;
    CString strLeftB, strRightB;
    {
        CString strLeft, strRight;
        int nIndex = strA.Find(_T(" "));
        if (nIndex > -1)
        {
            strLeft = strA.Left(nIndex);
            strRight = strA.Mid(nIndex+1,strA.GetLength() - nIndex-1);
 
            strLeft.Replace(_T("-"),_T(""));
            strRight.Replace(_T(":"),_T(""));
        }
 
        strLeftA = strLeft;
        strRightA = strRight;
    }
 
    {
        CString strLeft, strRight;
        int nIndex = strB.Find(_T(" "));
        if (nIndex > -1)
        {
            strLeft = strB.Left(nIndex);
            strRight = strB.Mid(nIndex+1,strB.GetLength() - nIndex-1);
 
            strLeft.Replace(_T("-"),_T(""));
            strRight.Replace(_T(":"),_T(""));
        }
 
        strLeftB = strLeft;
        strRightB = strRight;
    }
 
    __int64 nLeftA = _tstoi64(strLeftA);
    __int64 nLeftB = _tstoi64(strLeftB);
 
    __int64 nRightA = _tstoi64(strRightA);
    __int64 nRightB = _tstoi64(strRightB);
    if(nLeftA < nLeftB)
    {
        return true;
    }
    else if(nLeftA > nLeftB)
    {
        return false;
    }
    else
    {
        if(nRightA >= nRightB)
        {
            return false;
        }
 
        return true;
    }
 
    return true;
}

三、补充

除了比较大小,C++还可以实现计算日期相差多少天,下面是实现代码,希望对大家有所帮助

?
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
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
bool isLeap(int year) {
    return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
}
int main() {
    //定义好平年和闰年每月的天数
    int monthDays[13][2] = {
        {0,0},{31,31},{28,29},{30,30},{31,31},{30,30},
        {31,31},{30,30},{31,31},{30,30},{31,31},{30,30},
        {31,31}
    };
    int time1, year1, month1, days1;
    int time2, year2, month2, days2;
    int numbers =1;
    // 输入两个日期
    cout << "输入两个日期,空格分隔";
    cin >> time1 >> time2;
    if (time1>time2){
        int temp = time1;
        time1 = time2;
        time2 = temp;
 
    }
    //拆解日期,分为年,月,号
    year1 = time1 / 10000; month1 = time1 / 100 % 100; days1 = time1 % 100;
    year2 = time2 / 10000; month2 = time2 / 100 % 100; days2 = time2 % 100;
    //第一个日期 累加到 第二个日期
    while (year1 < year2 || month1 < month2 || days1 < days2) {
        days1++;// 在第一个日期基础上  加一天
        //加一天后,相应的月,年可能也要做一定的变化
        if (days1 == monthDays[month1][isLeap(year1)]+1) {//当前号超过当前月最高天数:月份加1,号变成下月的1号
            month1++;
            days1 = 1;
        }
        if (month1 == 13) {//月份超过12个月 :年份加1,月份变成下年的1月
            year1++;
            month1 = 1;
        }
        numbers++;
    }
    cout << numbers << endl;
    return 0;
}

到此这篇关于C++实现比较日期大小的示例代码的文章就介绍到这了,更多相关C++比较日期大小内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_37251750/article/details/129951190

延伸 · 阅读

精彩推荐
  • C/C++C++实现职工工资管理系统

    C++实现职工工资管理系统

    这篇文章主要为大家详细介绍了C++实现简单的职工工资管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    玛珈山大萌新11692022-10-10
  • C/C++Qt creator中项目的构建配置和运行设置的步骤

    Qt creator中项目的构建配置和运行设置的步骤

    使用 Qt Creator 集成开发环境构建和运行程序是一件非常简单的事情,一个按钮或者一个快捷键搞定全部,本文主要介绍了Qt creator中项目的构建配置和运行设...

    Uka Saegusa IN db6742022-03-06
  • C/C++C++编译原理之求解First集合

    C++编译原理之求解First集合

    这篇文章主要介绍的是C++/编译原理求解First集合,本文将围绕该话题详细展开全文,需要的小伙伴可以参考一下...

    立秋小猪5832022-01-25
  • C/C++在C语言中使用对数函数的方法

    在C语言中使用对数函数的方法

    这篇文章主要介绍了在C语言中使用对数函数的方法,包括以e为底和以10为底的对数计算,需要的朋友可以参考下...

    C语言教程网10472021-03-08
  • C/C++虚函数表-C++多态的实现原理解析

    虚函数表-C++多态的实现原理解析

    这篇文章主要介绍了虚函数表-C++多态的实现原理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    sherlock_lin6292021-10-21
  • C/C++C/C++ 中怎样使用SetConsoleTextAttribute()函数来控制输出字符的颜色

    C/C++ 中怎样使用SetConsoleTextAttribute()函数来控制输出字符的颜色

    这篇文章主要介绍了C/C++ 中如何使用SetConsoleTextAttribute()函数来控制输出字符的颜色,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一...

    Ridiculer5882021-10-28
  • C/C++C语言深入分析递归函数的实现

    C语言深入分析递归函数的实现

    递归(recursive)函数是“自己调用自己”的函数,无论是采用直接或间接调用方式。间接递归意味着函数调用另一个函数(然后可能又调用第三个函数等)...

    清风自在 流水潺潺9732022-11-10
  • C/C++C++ 手把手教你实现可变长的数组实现

    C++ 手把手教你实现可变长的数组实现

    这篇文章主要介绍了C++ 手把手教你实现可变长的数组实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    小林coding6102021-08-06