博客
关于我
求两个日期差值问题
阅读量:328 次
发布时间:2019-03-04

本文共 2195 字,大约阅读时间需要 7 分钟。

为了计算两个日期之间的天数,我们首先需要将输入的日期分解为年、月、日。然后,确定较小和较大的日期。接着,逐月、逐年地增加天数,直到达到较大的日期。需要考虑闰年的影响,尤其是2月的天数。通过循环处理每个月份的天数,当月份天数不足时,进入下一个月或年份。

步骤详解

  • 读取输入并分解日期

    • 读取两个日期字符串。
    • 分解为年、月、日。
  • 比较日期大小

    • 确定较小和较大的日期,作为开始和结束日期。
  • 初始化变量

    • sumDays 记录总天数。
    • month 数组存储每个月的天数。
  • 闰年判断

    • 编写函数 leapYear 判断闰年。
  • 月天数设置

    • 编写函数 setMonth 根据闰年设置2月的天数。
  • 计算天数

    • 使用循环逐月、逐年地增加天数,直到达到结束日期。
    • 处理月份和年份的变化,包括闰年影响的调整。
  • 输出结果

    • 打印计算得到的总天数。
  • 代码实现

    #include 
    #include
    using namespace std;bool leapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } else { return false; }}void setMonth(int year, int month[]) { if (leapYear(year)) { month[1] = 29; } else { month[1] = 28; }}void Compare(int &minDate, int &maxDate) { if (minDate > maxDate) { int temp = minDate; minDate = maxDate; maxDate = temp; }}int dateDifference(int month[], int year1, int year2, int month1, int month2, int day1, int day2) { int sumDays = 0; while (year1 != year2 || month1 != month2 || day1 != day2) { if (day2 <= month[month2]) { day2++; sumDays++; } else { if (month2 < 12) { month2++; day2 = 1; } else { year2++; setMonth(year2, month); month2 = 1; day2 = 1; } } } return sumDays;}int main() { int minDate, maxDate; cin >> minDate >> maxDate; Compare(minDate, maxDate); int maxYear = maxDate / 10000; int minYear = minDate / 10000; int month1 = (maxDate / 100) % 100; int month2 = (minDate / 100) % 100; int day1 = maxDate % 100; int day2 = minDate % 100; int month[13]; setMonth(minYear, month); int sumDays = dateDifference(month, maxYear, minYear, month1, month2, day1, day2); cout << sumDays << endl; return 0;}

    代码解释

  • leapYear 函数:判断给定年份是否为闰年。闰年的条件是能被4整除但不能被100整除,或者能被400整除。

  • setMonth 函数:根据闰年判断,设置2月的天数为29或28。

  • Compare 函数:交换两个日期的大小顺序,确保 minDate 小于 maxDate

  • dateDifference 函数:计算两个日期之间的天数。使用循环逐月、逐年增加天数,直到达到结束日期,并处理月份和年份的变化。

  • main 函数

    • 读取输入并分解为年、月、日。
    • 比较日期大小并交换顺序。
    • 初始化月份天数数组。
    • 调用 dateDifference 函数计算天数。
    • 输出结果。
  • 通过这种方法,可以准确计算两个日期之间的天数,包括处理闰年和跨年的情况。

    转载地址:http://dumq.baihongyu.com/

    你可能感兴趣的文章
    Oracle 11g超详细安装步骤
    查看>>
    Oracle 12c中的MGMTDB
    查看>>
    Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
    查看>>
    Oracle 9i数据库管理教程
    查看>>
    ORACLE Active dataguard 一个latch: row cache objects BUG
    查看>>
    oracle avg、count、max、min、sum、having、any、all、nvl的用法
    查看>>
    Oracle BEQ方式连接配置
    查看>>
    oracle Blob保存方式,oracle 存储过程操作blob
    查看>>
    Oracle BMW Racing sailing vessel帆船图
    查看>>
    ORACLE Bug 4431215 引发的血案—原因分析篇
    查看>>
    Oracle cmd乱码
    查看>>
    Oracle Corp甲骨文公司推出Oracle NoSQL数据库2.0版
    查看>>
    oracle dblink 创建使用 垮库转移数据
    查看>>
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    查看>>
    Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
    查看>>
    oracle dg switchover,DG Switchover fails
    查看>>
    Oracle E-Business Suite软件 任意文件上传漏洞(CVE-2022-21587)
    查看>>
    Oracle EBS OPM 发放生产批
    查看>>
    Oracle EBS-SQL (BOM-15):检查多层BOM(含common BOM).sql
    查看>>
    Oracle EBS环境下查找数据源(OAF篇)
    查看>>