C++實(shí)現(xiàn)比較日期大小的示例代碼
一、目的
用來(lái)比較兩個(gè)日期。日期格式: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; }
三、補(bǔ)充
除了比較大小,C++還可以實(shí)現(xiàn)計(jì)算日期相差多少天,下面是實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助
#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; // 輸入兩個(gè)日期 cout << "輸入兩個(gè)日期,空格分隔"; cin >> time1 >> time2; if (time1>time2){ int temp = time1; time1 = time2; time2 = temp; } //拆解日期,分為年,月,號(hào) year1 = time1 / 10000; month1 = time1 / 100 % 100; days1 = time1 % 100; year2 = time2 / 10000; month2 = time2 / 100 % 100; days2 = time2 % 100; //第一個(gè)日期 累加到 第二個(gè)日期 while (year1 < year2 || month1 < month2 || days1 < days2) { days1++;// 在第一個(gè)日期基礎(chǔ)上 加一天 //加一天后,相應(yīng)的月,年可能也要做一定的變化 if (days1 == monthDays[month1][isLeap(year1)]+1) {//當(dāng)前號(hào)超過(guò)當(dāng)前月最高天數(shù):月份加1,號(hào)變成下月的1號(hào) month1++; days1 = 1; } if (month1 == 13) {//月份超過(guò)12個(gè)月 :年份加1,月份變成下年的1月 year1++; month1 = 1; } numbers++; } cout << numbers << endl; return 0; }
到此這篇關(guān)于C++實(shí)現(xiàn)比較日期大小的示例代碼的文章就介紹到這了,更多相關(guān)C++比較日期大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C語(yǔ)言編程深入理解取整取余取模問(wèn)題示例分析
這篇文章主要為大家介紹了C語(yǔ)言編程深入理解取整取余取模問(wèn)題的示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11探討編寫(xiě)int strlen(char *strDest);不允許定義變量的問(wèn)題
本篇文章是對(duì)編寫(xiě)int strlen(char *strDest);不允許定義變量的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++淺析類(lèi)與對(duì)象基礎(chǔ)點(diǎn)
類(lèi)和對(duì)象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱(chēng)。對(duì)象是對(duì)客觀事物的抽象,類(lèi)是對(duì)對(duì)象的抽象。類(lèi)是一種抽象的數(shù)據(jù)類(lèi)型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域2022-07-07C++實(shí)現(xiàn)DES加密算法實(shí)例解析
這篇文章主要介紹了C++實(shí)現(xiàn)DES加密算法實(shí)例解析,是一個(gè)很實(shí)用的功能,需要的朋友可以參考下2014-08-08C++實(shí)現(xiàn)基數(shù)排序的方法詳解
本篇文章是對(duì)使用C++實(shí)現(xiàn)基數(shù)排序的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05