一、目的
用来比较两个日期。日期格式: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;
}