C++怎么实现比较日期大小

其他教程   发布日期:2024年11月29日   浏览次数:203

今天小编给大家分享一下C++怎么实现比较日期大小的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、目的

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

二、代码

  1. //std::wstring strA = L"2023-03-31 09:16:56";
  2. //std::wstring strB = L"2023-03-31 09:21:34";
  3. bool LessThanEx(std::wstring strA, std::wstring strB)
  4. {
  5. std::wstring strLeftA, strRightA;
  6. std::wstring strLeftB, strRightB;
  7. {
  8. std::wstring strLeft, strRight;
  9. std::size_t nIndex = strA.find(L" ");
  10. if (nIndex!=std::string::npos)
  11. {
  12. strLeft = strA.substr(0,nIndex);
  13. strRight = strA.substr(nIndex+1);
  14. std::wstring wsDivide = L"-";
  15. strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
  16. strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
  17. wsDivide = L":";
  18. strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
  19. strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
  20. }
  21. strLeftA = strLeft;
  22. strRightA = strRight;
  23. }
  24. {
  25. std::wstring strLeft, strRight;
  26. std::size_t nIndex = strB.find(L" ");
  27. if (nIndex!=std::string::npos)
  28. {
  29. strLeft = strB.substr(0,nIndex);
  30. strRight = strB.substr(nIndex+1);
  31. std::wstring wsDivide = L"-";
  32. strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
  33. strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
  34. wsDivide = L":";
  35. strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
  36. strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
  37. }
  38. strLeftB = strLeft;
  39. strRightB = strRight;
  40. }
  41. __int64 nLeftA = std::stoi(strLeftA);
  42. __int64 nLeftB = std::stoi(strLeftB);
  43. __int64 nRightA = std::stoi(strRightA);
  44. __int64 nRightB = std::stoi(strRightB);
  45. if(nLeftA < nLeftB)
  46. {
  47. return true;
  48. }
  49. else if(nLeftA > nLeftB)
  50. {
  51. return false;
  52. }
  53. else
  54. {
  55. if(nRightA >= nRightB)
  56. {
  57. return false;
  58. }
  59. return true;
  60. }
  61. return true;
  62. }
  63. //CString strA = _T("2023-03-31 09:16:56");
  64. //CString strB = _T("2023-03-31 09:21:34");
  65. bool LessThan(CString strA, CString strB)
  66. {
  67. CString strLeftA, strRightA;
  68. CString strLeftB, strRightB;
  69. {
  70. CString strLeft, strRight;
  71. int nIndex = strA.Find(_T(" "));
  72. if (nIndex > -1)
  73. {
  74. strLeft = strA.Left(nIndex);
  75. strRight = strA.Mid(nIndex+1,strA.GetLength() - nIndex-1);
  76. strLeft.Replace(_T("-"),_T(""));
  77. strRight.Replace(_T(":"),_T(""));
  78. }
  79. strLeftA = strLeft;
  80. strRightA = strRight;
  81. }
  82. {
  83. CString strLeft, strRight;
  84. int nIndex = strB.Find(_T(" "));
  85. if (nIndex > -1)
  86. {
  87. strLeft = strB.Left(nIndex);
  88. strRight = strB.Mid(nIndex+1,strB.GetLength() - nIndex-1);
  89. strLeft.Replace(_T("-"),_T(""));
  90. strRight.Replace(_T(":"),_T(""));
  91. }
  92. strLeftB = strLeft;
  93. strRightB = strRight;
  94. }
  95. __int64 nLeftA = _tstoi64(strLeftA);
  96. __int64 nLeftB = _tstoi64(strLeftB);
  97. __int64 nRightA = _tstoi64(strRightA);
  98. __int64 nRightB = _tstoi64(strRightB);
  99. if(nLeftA < nLeftB)
  100. {
  101. return true;
  102. }
  103. else if(nLeftA > nLeftB)
  104. {
  105. return false;
  106. }
  107. else
  108. {
  109. if(nRightA >= nRightB)
  110. {
  111. return false;
  112. }
  113. return true;
  114. }
  115. return true;
  116. }

三、补充

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

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. using namespace std;
  5. bool isLeap(int year) {
  6. return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
  7. }
  8. int main() {
  9. //定义好平年和闰年每月的天数
  10. int monthDays[13][2] = {
  11. {0,0},{31,31},{28,29},{30,30},{31,31},{30,30},
  12. {31,31},{30,30},{31,31},{30,30},{31,31},{30,30},
  13. {31,31}
  14. };
  15. int time1, year1, month2, days1;
  16. int time2, year2, month3, days2;
  17. int numbers =1;
  18. // 输入两个日期
  19. cout << "输入两个日期,空格分隔";
  20. cin >> time1 >> time2;
  21. if (time1>time2){
  22. int temp = time1;
  23. time1 = time2;
  24. time2 = temp;
  25. }
  26. //拆解日期,分为年,月,号
  27. year1 = time1 / 10000; month2 = time1 / 100 % 100; days1 = time1 % 100;
  28. year2 = time2 / 10000; month3 = time2 / 100 % 100; days2 = time2 % 100;
  29. //第一个日期 累加到 第二个日期
  30. while (year1 < year2 || month2 < month3 || days1 < days2) {
  31. days1++;// 在第一个日期基础上 加一天
  32. //加一天后,相应的月,年可能也要做一定的变化
  33. if (days1 == monthDays[month2][isLeap(year1)]+1) {//当前号超过当前月最高天数:月份加1,号变成下月的1号
  34. month2++;
  35. days1 = 1;
  36. }
  37. if (month2 == 13) {//月份超过12个月 :年份加1,月份变成下年的1月
  38. year1++;
  39. month2 = 1;
  40. }
  41. numbers++;
  42. }
  43. cout << numbers << endl;
  44. return 0;
  45. }

以上就是C++怎么实现比较日期大小的详细内容,更多关于C++怎么实现比较日期大小的资料请关注九品源码其它相关文章!