您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编给大家分享一下C++怎么实现比较日期大小的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
用来比较两个日期。日期格式: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++还可以实现计算日期相差多少天,下面是实现代码,希望对大家有所帮助
#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, month2, days1; int time2, year2, month3, days2; int numbers =1; // 输入两个日期 cout << "输入两个日期,空格分隔"; cin >> time1 >> time2; if (time1>time2){ int temp = time1; time1 = time2; time2 = temp; } //拆解日期,分为年,月,号 year1 = time1 / 10000; month2 = time1 / 100 % 100; days1 = time1 % 100; year2 = time2 / 10000; month3 = time2 / 100 % 100; days2 = time2 % 100; //第一个日期 累加到 第二个日期 while (year1 < year2 || month2 < month3 || days1 < days2) { days1++;// 在第一个日期基础上 加一天 //加一天后,相应的月,年可能也要做一定的变化 if (days1 == monthDays[month2][isLeap(year1)]+1) {//当前号超过当前月最高天数:月份加1,号变成下月的1号 month2++; days1 = 1; } if (month2 == 13) {//月份超过12个月 :年份加1,月份变成下年的1月 year1++; month2 = 1; } numbers++; } cout << numbers << endl; return 0; }
以上就是“C++怎么实现比较日期大小”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。