| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4114 人关注过本帖
标题:[求助]怎么实现两个日期相减
只看楼主 加入收藏
xujianxiang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-8-7
收藏
 问题点数:0 回复次数:10 
[求助]怎么实现两个日期相减

怎么在VC中实现两个日期相减,相减后得到天数?????????谢谢回复.

搜索更多相关主题的帖子: 天数 
2006-08-07 16:07
xujianxiang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-8-7
收藏
得分:0 
顶一下
2006-08-07 18:48
lxlgod
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-8-9
收藏
得分:0 
用结构体来记录日期数据不就行了
2006-08-09 19:14
heliujin
Rank: 2
等 级:论坛游民
帖 子:249
专家分:14
注 册:2006-3-14
收藏
得分:0 
今天困了 最晚后天晚上给你回复  不好意思啊
2006-08-09 20:41
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 

正好我写过一个类,你看看行不:
[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]


for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-08-10 00:13
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 

上面的是头文件,这里是实现:
[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]


for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-08-10 00:14
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 

示例:
[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]


for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-08-10 00:14
yeshirow
Rank: 4
等 级:贵宾
威 望:10
帖 子:854
专家分:0
注 册:2006-6-8
收藏
得分:0 
還要自己寫實現代碼啊??

有沒有像 VB 中的 DateDiff 函數之類的.

我想引用 msvbvm60.dll, 然後使用裏面的方法, 不知道在 VC 怎麽引用.

原來朋友仔感情再天真, 亦是我永遠也會愛惜的人, 明日愛他人, 也記住學會不要緊; 原來朋友比戀人更高分, 亦讓我開始懂得不記恨, 若大家都敏感, 我更要永遠記得拒絕再因小事怪人, 爲何沒有這條校訓...Twins-朋友仔 MCSD Training
2006-08-10 00:45
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 
这个代码也不长啊~~~ 你参考C++标准库看看,说不定可能有~~

for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-08-10 12:14
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
得分:0 
够专业的代码

2006-08-11 13:51
快速回复:[求助]怎么实现两个日期相减
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012368 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved