在C+中获得月底前的秒数+; 我有一个要求,在C++中,直到月底才能获得时间间隔。有没有C++的API可以轻易地为我做?

在C+中获得月底前的秒数+; 我有一个要求,在C++中,直到月底才能获得时间间隔。有没有C++的API可以轻易地为我做?,c++,time,C++,Time,我需要启动一个计时器,它将在下个月的第一天00:00:00过期。为此,我需要计算时间间隔,即从现在到本月底的秒数 它的方向相当直。获取现在与当月最后一天之间的天数差异。然后获取一天中的秒数 现在你有了截至月底的秒数,现在用截至今天的秒数减去它,你将得到截至月底的秒数 #include <time.h> #include <iostream> using namespace std; int main(){ int day1,month1,year1;

我需要启动一个计时器,它将在下个月的第一天00:00:00过期。为此,我需要计算时间间隔,即从现在到本月底的秒数

它的方向相当直。获取现在与当月最后一天之间的天数差异。然后获取一天中的秒数

现在你有了截至月底的秒数,现在用截至今天的秒数减去它,你将得到截至月底的秒数

#include <time.h>
#include <iostream>
using namespace std;

int main(){

    int day1,month1,year1;
    int day2,month2,year2;
    int i,temp,DaysDiff=0;
    int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
    int totalDiffDaysSecs=0;
    int nowSeconds=0;
    int expire=1000;

    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );

    cout<<"\n";
    day1=now->tm_mday;
    month1=(now->tm_mon + 1);
    year1=(now->tm_year + 1900);

    day2=31;
    month2=7;
    year2=2015;
    temp=day1;

    for(i=month1;i<month2+(year2-year1)*12;i++){
        if(i>12){
            i=1;
            year1++;
        }

        if(i==2){
            if(year1%4==0 && (year1%100!=0 || year1%400==0))
            month[i-1]=29;
          else
            month[i-1]=28;
        }

        DaysDiff=DaysDiff+(month[i-1]-temp);
        temp=0;

    }
    cout <<"Current Time = "<< now->tm_hour << ":" << now->tm_min << "."<<now->tm_sec<<"\n";
    cout <<"Today's date   "<< (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-'   <<  now->tm_mday << " \n";
    cout <<"Target  date   "<< year2 << '-' << month2 << '-'   <<  day2 << " \n";

    DaysDiff=DaysDiff+day2-temp;

    cout<<"Target Month = "<<i<<"\n";
    cout<<"Days in Target month = "<<month[i-1]<<"\n";
    cout<<"Days diff Today - Target Month = "<<DaysDiff<<" \n";

    totalDiffDaysSecs=DaysDiff*24*60*60; 

    nowSeconds= (now->tm_hour  * 3600) + (now->tm_min * 60)+ now->tm_sec;

    cout<<"Total seconds in a day = "<<24*60*60<<" \n";
    cout<<"current total seconds so far today = "<< nowSeconds <<"\n";

    cout<<"Total number of seconds in "<< DaysDiff <<" days = "<< totalDiffDaysSecs <<"\n";
    cout<<"\n\n";

    while(expire>0){

       t = time(0);   // get time now
       now = localtime( & t ); 

       nowSeconds= (now->tm_hour  * 3600) + (now->tm_min * 60)+ now->tm_sec;

       expire=totalDiffDaysSecs - nowSeconds;

       cout <<"Seconds until end of month = "<< expire <<"   \r";   
    }


    cout<<"\n\n";
    cout<<"Countdown expired.\n\n";
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int day1,month1,year1;
int第2天,第2个月,第2年;
int i,temp,DaysDiff=0;
内月[]={31,28,31,30,31,30,31,31,30,31,31};
int totalDiffDaysSecs=0;
整数秒=0;
int=1000;
time_t=time(0);//立即获取时间
struct tm*now=localtime(&t);
couttm_mon+1);
年份1=(现在->tm_年份+1900);
第2天=31天;
month2=7;
第2年=2015年;
温度=第1天;
对于(i=month1;i12){
i=1;
第1年++;
}
如果(i==2){
如果(第1年%4==0&(第1年%100!=0 | |第1年%400==0))
月[i-1]=29;
其他的
月[i-1]=28;
}
DaysDiff=DaysDiff+(月[i-1]-温度);
温度=0;
}

看不到标题。看一看你是在寻找精确的输出,还是一个合理的近似值?我需要启动一个计时器,它将在下个月的第一天00:00:00过期。为此,我需要计算时间间隔,即从现在到本月底的秒数。我能为你做很多事。