C++ 計算任意兩個日期之間的天數
阿新 • • 發佈:2018-04-07
gpo start plain efault ediff include win sel put
C++寫的一個計算兩個日期之間天數的小程序:
[cpp] view plain copy
- #include <Windows.h>
- #include <stdio.h>
- struct tagDate
- {
- int year;
- int month;
- int day;
- };
- //設置日期
- void SetDate(int y, int m, int d, tagDate *date)
- {
- date->year = y;
- date->month = m;
- date->day = d;
- }
- //是否閏年
- int IsLeapYear(int year)
- {
- return ((year%4==0) && (year%100!=0)||year%400==0);
- }
- //得到date.month的最大天數
- int GetLastDay(tagDate date)
- {
- int num;
- switch(date.month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- num=31;
- break;
- case 2:
- num = 28+IsLeapYear(date.year);
- break;
- default:
- num = 30;
- }
- return num;
- }
- //日期是否合法
- int IsDateValid(tagDate date)
- {
- if(date.year <0 || date.month <1 || date.month> 12)
- return 0;
- if(date.day <1 || date.day> GetLastDay(date))
- return 0;
- return 1;
- }
- //date+1
- void AddDay(tagDate *date)
- {
- date->day++;
- if(date->day > GetLastDay(*date))
- {
- date->day = 1;
- date->month++;
- if(date->month > 12)
- {
- date->month = 1;
- date->year++;
- }
- }
- }
- //date1比date2小返回值為1,否則為0
- int Compare(tagDate date1, tagDate date2)
- {
- if(date1.year < date2.year)
- return 1;
- if(date1.year <= date2.year && date1.month < date2.month)
- return 1;
- if(date1.year <= date2.year && date1.month <= date2.month && date1.day < date2.day)
- return 1;
- return 0;
- }
- //計算兩個日期的間隔天數
- long DateDiff(tagDate date1, tagDate date2)
- {
- long delta = 0;
- tagDate date3;
- //若date1 > date2,交換date1,date2
- if(!Compare(date1, date2))
- {
- date3 = date2;
- date2 = date1;
- date1 = date3;
- }
- //date1比date2少時,date1日期加1
- while(Compare(date1,date2))
- {
- AddDay(&date1);
- delta++;
- }
- return delta;
- }
- void main()
- {
- tagDate date1, date2;
- int y, m, d;
- system("cls");
- while(1)
- {
- puts("\nInput date1\t ");
- scanf("%d,%d,%d", &y, &m, &d);
- SetDate(y, m, d, &date1);
- if(!IsDateValid(date1))
- {
- puts( "date1 is invalid !");
- exit(1);
- }
- puts("\nInput date2\t ");
- scanf("%d,%d,%d", &y, &m, &d);
- SetDate(y, m, d, &date2);
- if(!IsDateValid(date2))
- {
- puts( "date2 is invalid !");
- exit(1);
- }
- //
- printf("\nResult : [%ld]", DateDiff(date1,date2));
- }
- }
https://blog.csdn.net/hellokandy/article/details/52194332
C++ 計算任意兩個日期之間的天數