C++實現(xiàn)比較日期大小的示例代碼
更新時間:2023年04月04日 15:29:43 作者:歐特克_Glodon
這篇文章主要為大家詳細(xì)介紹了如何使用C++實現(xiàn)比較日期大小的功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的可以了解一下
一、目的
用來比較兩個日期。日期格式:2023-03-31 09:16:56。
二、代碼
//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++還可以實現(xiàn)計算日期相差多少天,下面是實現(xiàn)代碼,希望對大家有所幫助
#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() {
//定義好平年和閏年每月的天數(shù)
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++;// 在第一個日期基礎(chǔ)上 加一天
//加一天后,相應(yīng)的月,年可能也要做一定的變化
if (days1 == monthDays[month1][isLeap(year1)]+1) {//當(dāng)前號超過當(dāng)前月最高天數(shù):月份加1,號變成下月的1號
month1++;
days1 = 1;
}
if (month1 == 13) {//月份超過12個月 :年份加1,月份變成下年的1月
year1++;
month1 = 1;
}
numbers++;
}
cout << numbers << endl;
return 0;
}
到此這篇關(guān)于C++實現(xiàn)比較日期大小的示例代碼的文章就介紹到這了,更多相關(guān)C++比較日期大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探討編寫int strlen(char *strDest);不允許定義變量的問題
本篇文章是對編寫int strlen(char *strDest);不允許定義變量的問題進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

