C++实现比较日期大小的示例代码

 更新时间:2023年04月04日 15:29:43   作者:欧特克_Glodon  
这篇文章主要为大家详细介绍了如何使用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, 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++;// 在第一个日期基础上  加一天
		//加一天后,相应的月,年可能也要做一定的变化
		if (days1 == monthDays[month1][isLeap(year1)]+1) {//当前号超过当前月最高天数:月份加1,号变成下月的1号
			month1++;
			days1 = 1;
		}
		if (month1 == 13) {//月份超过12个月 :年份加1,月份变成下年的1月
			year1++;
			month1 = 1;
		}
		numbers++;
	}
	cout << numbers << endl;
	return 0;
}

到此这篇关于C++实现比较日期大小的示例代码的文章就介绍到这了,更多相关C++比较日期大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++实现宾馆房间管理系统

    C++实现宾馆房间管理系统

    这篇文章主要为大家详细介绍了C++实现宾馆房间管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • c++超细致讲解引用

    c++超细致讲解引用

    在我们日常的生活中每个人都或多或少存在一个"外号",例如《西游记》中孙悟空就有诸多外号:美猴王,孙行者,齐天大圣等等。那么在C++中,也可以给一个已经存在的变量取别名,这就是引用。那么接下来深入来探讨一下引用
    2022-05-05
  • C/C++ Qt ToolBar菜单组件的具体使用

    C/C++ Qt ToolBar菜单组件的具体使用

    ToolBar工具栏在所有窗体应用程序中都广泛被使用,使用ToolBar可以很好的规范菜单功能分类,本文就详细的介绍一下ToolBar组件的应用,感兴趣的可以了解一下
    2021-11-11
  • c++ class中成员与分配内存的问题详解

    c++ class中成员与分配内存的问题详解

    很多人都知道C++类是由结构体发展得来的,所以他们的成员变量(C语言的结构体只有成员变量)的内存分配机制是一样的,下面这篇文章主要给大家介绍了关于c++ class中成员与分配内存问题的相关资料,需要的朋友可以参考下
    2021-10-10
  • C语言中使用快速排序算法对元素排序的实例详解

    C语言中使用快速排序算法对元素排序的实例详解

    这篇文章主要介绍了C语言中使用快速排序算法对元素排序的实例详解,文中细分了几个情况来举例,在注释里有说明,需要的朋友可以参考下
    2016-04-04
  • C语言求解定积分的方法

    C语言求解定积分的方法

    这篇文章主要为大家详细介绍了C语言求解定积分的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C++中的std::initializer_list使用解读

    C++中的std::initializer_list使用解读

    这篇文章主要介绍了C++中的std::initializer_list使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • C++中BitBlt的使用方法详解

    C++中BitBlt的使用方法详解

    这篇文章主要介绍了C++中BitBlt的使用方法详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • C++11中的可变参数模板/lambda表达式

    C++11中的可变参数模板/lambda表达式

    C++11的新特性可变参数模板能够让我们创建可以接受可变参数的函数模板和类模板,相比C++98和C++03,类模板和函数模板中只能含固定数量的模板参数,可变参数模板无疑是一个巨大的改进,这篇文章主要介绍了C++11中的可变参数模板/lambda表达式,需要的朋友可以参考下
    2023-03-03
  • 详谈全排列next_permutation() 函数的用法(推荐)

    详谈全排列next_permutation() 函数的用法(推荐)

    下面小编就为大家带来一篇详谈全排列next_permutation() 函数的用法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03

最新评论