怎么在VC中实现两个日期相减,相减后得到天数?????????谢谢回复.
正好我写过一个类,你看看行不:
[CODE]
/*********************************************
*
* Date.cpp
*
* Create Date : 2006-03-25
* Modify Date : 2006-03-26
*
*********************************************/
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;
class Date {
int year,month,day;
void addOneDay(); //增加1天
void subOneDay(); //减少1天
int subSmallDate(const Date &date) const; //减去一个更小的 Date,返回天数
public:
Date(int y=0/*year*/ ,int m=0/*month*/, int d=0/*day*/);
int daysPerMonth(int m=-1) const; //每月多少天?
int daysPerYear(int y=-1) const; //每年多少天?
int compare(const Date &date) const; //与string中的compare差不多,相等返回0
bool isLeapYear(int y=-1) const; //是否闰年
int subDate(const Date &date) const; //减去一个日期,返回天数
Date subDays(int days) const; //减少指定的天数
Date addDays(int days) const; //增加指定的天数
void prtMsg(int m=1,int d=21) const; //输出日期
/********** Operator Overloading ***********/
Date& operator++(); //++ Date
Date operator++(int); //Date ++
Date& operator--(); //-- Date
Date operator--(int); //Date --
Date operator+(int days); //Date + days
Date operator-(int days); //Date - days
int operator-(const Date &date); //Date1 - Date2
friend ostream& operator<<(ostream &ostrm,const Date &date) { //重载<<
ostrm<<"今天是:"<<date.year<<" 年(";
if(date.isLeapYear())
ostrm<<"闰";
else ostrm<<"平";
ostrm<<"年) "<<date.month<<" 月 "<<date.day<<" 日。";
return ostrm;
}
};
#endif //DATE_H
[/CODE]
上面的是头文件,这里是实现:
[CODE]
/*********************************************
*
* IM_Date.cpp
*
* Create Date : 2006-03-25
* Modify Date : 2006-03-26
*
*********************************************/
#include "Date.h"
#include <iostream>
using namespace std;
/*************** Private Methods ****************/
void Date::addOneDay() {
if(++day > daysPerMonth()) {
day=1;
month++;
}
if(month>12) {
month=1;
year++;
}
}
void Date::subOneDay() {
if(--day < 1) {
if(--month < 1) {
month=12;
year--;
}
day=daysPerMonth();
}
}
int Date::subSmallDate(const Date &dat) const {
int days=0;
Date date(dat);
while(year>(date.year+1)) {
days+=date.daysPerYear();
date.year++;
}
while(month>(date.month+1)) {
days+=date.daysPerMonth();
date.month++;
}
while(day>(date.day+1)) {
days++;
date.day++;
}
while(compare(date)>0) {
days++;
date.addOneDay();
}
return days;
}
/*************** Public Methods ****************/
Date::Date(int y,int m,int d) : year(y),month(m),day(d) {
if(year<0) {
cout<<"*Error : Year "<<year<<" can't be negative..."<<endl;
delete this;
}
else if(month<1 || month>12) {
cout<<"*Error : "<<month<<" is a wrong Month..."<<endl;
delete this;
}
else if(day<1 || day>daysPerMonth()) {
cout<<"*Error : "<<day<<" is a wrong Day by Month "<<month<<" ..."<<endl;
delete this;
}
}
int Date::daysPerMonth(int m) const {
m=(m<0)?month:m;
switch(m) {
case 1: return 31;
case 2: return isLeapYear(year) ? 29 : 28;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
default: return -1;
}
}
int Date::daysPerYear(int y) const {
y=(y<0)?year:y;
if(isLeapYear(y))
return 366;
return 365;
}
int Date::compare(const Date &date) const {
if(year>date.year)
return 1;
else if(year<date.year)
return -1;
else {
if(month>date.month)
return 1;
else if(month<date.month)
return -1;
else {
if(day>date.day)
return 1;
else if(day<date.day)
return -1;
else return 0;
}
}
}
bool Date::isLeapYear(int y) const {
y=(y<0)?year:y;
if(0==y%400 || (0==y%4 && 0!=y%100))
return true;
return false;
}
int Date::subDate(const Date &date) const {
if(compare(date)>0)
return subSmallDate(date);
else if(compare(date)<0)
return -(date.subSmallDate(*this));
else return 0;
}
Date Date::addDays(int days) const {
Date newDate(year,month,day);
if(days>0) {
for(int i=0;i<days;i++)
newDate.addOneDay();
}
else if (days<0) {
for(int i=0;i<(-days);i++)
newDate.subOneDay();
}
return newDate;
}
Date Date::subDays(int days) const {
return addDays(-days);
}
/********** Operator Overloading ***********/
Date& Date::operator++() { //++Date
addOneDay();
return *this;
}
Date Date::operator++(int) { //Date++
Date date(*this);
addOneDay();
return date;
}
Date& Date::operator--() { //--Date
subOneDay();
return *this;
}
Date Date::operator--(int) { //Date--
Date date(*this);
subOneDay();
return date;
}
Date Date::operator+(int days) { //Date+days
return addDays(days);
}
Date Date::operator-(int days) { //Date-days
return subDays(days);
}
int Date::operator-(const Date &date) { //Date1-Date2
return subDate(date);
}
void Date::prtMsg(int m,int d) const {
cout<<"今天是:"<<year<<" 年(";
if(isLeapYear())
cout<<"闰";
else cout<<"平";
cout<<"年) "<<month<<" 月 "<<day<<" 日";
if(m==month && d==day)
cout<<",生日快乐!";
else cout<<"。";
cout<<endl;
}
[/CODE]
示例:
[CODE]
/*********************************************
*
* IM_Date.cpp
*
* Creat Date : 2006-03-25
* Modify Date: 2006-03-26
*
*********************************************/
#include "Date.h"
#include <iostream>
using namespace std;
int main() {
Date date1(2006,6,20);
Date date2(1986,11,5);
Date date3(1986,2,18);
Date date4(1987,8,1);
//date2.subDays(7003).prtMsg();
//date1.prtMsg();
//cout<<date1<<endl;
cout<<date1-date2<<endl;
cout<<date1-date3<<endl;
cout<<date1-date4<<endl;
return 0;
}
[/CODE]