在c+中为日期添加天数+; 我是C++新手,所以我想知道是否有一些库可以帮助处理更流畅的日期。
我的任务相当简单。我有一个不同值的开始日期,我必须得到当我将日期增加随机天数时的日期 我认为在c+中为日期添加天数+; 我是C++新手,所以我想知道是否有一些库可以帮助处理更流畅的日期。,c++,date,C++,Date,我的任务相当简单。我有一个不同值的开始日期,我必须得到当我将日期增加随机天数时的日期 我认为mktime和time\t对象对我正在尝试做的事情很有帮助。如果他们是答案,有人能给我一个好指南的链接吗? 促进: Qt框架: 代码项目托管: 如果您想自己处理日期和时间: 促进: Qt框架: 代码项目托管: 如果您想自己处理日期和时间: 一天通常是86400秒(除此之外)。您可以将其添加到time\u t并获得新的time\u t等。然后您可以使用mktime和localtime将其转换为struct
mktime
和time\t
对象对我正在尝试做的事情很有帮助。如果他们是答案,有人能给我一个好指南的链接吗?- 促进:
- Qt框架:
- 代码项目托管:
- 如果您想自己处理日期和时间:
- 促进:
- Qt框架:
- 代码项目托管:
- 如果您想自己处理日期和时间:
time\u t
并获得新的time\u t
等。然后您可以使用mktime
和localtime
将其转换为struct tm
,它可以通过strftime
显示,也可以通过strptime进行解析一天通常为86400秒(除此之外)。您可以将其添加到time\u t
中,并获得一个新的time\u t
等。然后您可以使用mktime
和localtime
将其转换为struct tm
,它可以通过strftime
显示,也可以通过strptime进行解析。当然,如果您的编译器足够新,那么就有C++11名称空间。嗯,有两种。当然,如果您的编译器足够新,那么就有了C++11名称空间。我刚刚编写了自己的函数,将天、月和年添加到现有的日期类中。我还不能测试它,但也许它有帮助:
bool DATE::add(int Day, int Month, int Year){
int DaysPerMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
this -> Day += Day;
while(this -> Day > DaysPerMonth[ this-> Month ]){
if((this -> Year % 4 == 0 && this -> Year % 100 != 0) || this -> Year % 400 == 0){
DaysPerMonth[2] = 29;
}
this -> Day -= DaysPerMonth[ this-> Month ];
this -> Month++;
if(this -> Month > 12){
this -> Month = 1;
this -> Year++;
}
}
this -> Month = ( this -> Month + (Month % 12));
this -> Year = ( this -> Year + Year + (Month/12));
if((this -> Year % 4 == 0 && this -> Year % 100 != 0) || this -> Year % 400 == 0){
DaysPerMonth[2] = 29;
// check pathologic case wether date is 1 of March and added Year targets switchyear
if( this -> Day == 1 && this -> Month == 3){
this -> Day = 29;
this -> Month = 2;
}
}
if(this -> Month < 1 || this -> Month > 12 || this -> Day < 1 || this -> Day > DaysPerMonth[this->Month]){
valid = false;
cerr << "something went wrong, calculated Date is: " << this -> Day << "."<< this -> Month << "." << this -> Year << endl << flush;
return false;
}else{
return true;
}
bool DATE::add(整数天、整数月、整数年){
int DaysPerMonth[13]={0,31,28,31,30,31,30,30,31,31,31,30,31};
本->天+=天;
而(本->日->日/月[本->月]){
如果((本->年%4==0&&本->年%100!=0)|本->年%400==0){
日/月[2]=29;
}
本->日-=DaysPerMonth[本->月];
本->月++;
如果(本->月>12){
本->月=1;
今年++;
}
}
本->月=(本->月+(月%12));
本->年=(本->年+年+(月/12));
如果((本->年%4==0&&本->年%100!=0)|本->年%400==0){
日/月[2]=29;
//检查病理病例日期是否为3月1日,以及是否增加了年度目标
如果(本->日==1&&本->月==3){
今天=29天;
本->月=2;
}
}
如果(本->月<1 | |本->月>12 | |本->日<1 | |本->日>月[本->月]{
有效=错误;
cerr我刚刚编写了我自己的函数,将天、月和年添加到现有的日期类中。我还不能测试它,但它可能有帮助:
bool DATE::add(int Day, int Month, int Year){
int DaysPerMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
this -> Day += Day;
while(this -> Day > DaysPerMonth[ this-> Month ]){
if((this -> Year % 4 == 0 && this -> Year % 100 != 0) || this -> Year % 400 == 0){
DaysPerMonth[2] = 29;
}
this -> Day -= DaysPerMonth[ this-> Month ];
this -> Month++;
if(this -> Month > 12){
this -> Month = 1;
this -> Year++;
}
}
this -> Month = ( this -> Month + (Month % 12));
this -> Year = ( this -> Year + Year + (Month/12));
if((this -> Year % 4 == 0 && this -> Year % 100 != 0) || this -> Year % 400 == 0){
DaysPerMonth[2] = 29;
// check pathologic case wether date is 1 of March and added Year targets switchyear
if( this -> Day == 1 && this -> Month == 3){
this -> Day = 29;
this -> Month = 2;
}
}
if(this -> Month < 1 || this -> Month > 12 || this -> Day < 1 || this -> Day > DaysPerMonth[this->Month]){
valid = false;
cerr << "something went wrong, calculated Date is: " << this -> Day << "."<< this -> Month << "." << this -> Year << endl << flush;
return false;
}else{
return true;
}
bool DATE::add(整数天、整数月、整数年){
int DaysPerMonth[13]={0,31,28,31,30,31,30,30,31,31,31,30,31};
本->天+=天;
而(本->日->日/月[本->月]){
如果((本->年%4==0&&本->年%100!=0)|本->年%400==0){
日/月[2]=29;
}
本->日-=DaysPerMonth[本->月];
本->月++;
如果(本->月>12){
本->月=1;
今年++;
}
}
本->月=(本->月+(月%12));
本->年=(本->年+年+(月/12));
如果((本->年%4==0&&本->年%100!=0)|本->年%400==0){
日/月[2]=29;
//检查病理病例日期是否为3月1日,以及是否增加了年度目标
如果(本->日==1&&本->月==3){
今天=29天;
本->月=2;
}
}
如果(本->月<1 | |本->月>12 | |本->日<1 | |本->日>月[本->月]{
有效=错误;
瑟尔