首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

把秒数转天数,怎么转换成日期 C++

2012-09-08 
把秒数转天数,怎样转换成日期 C++我从客户的一个表格里读取日期,结果2003-10-28,2003-10-29日分别变成了37

把秒数转天数,怎样转换成日期 C++
我从客户的一个表格里读取日期,结果2003-10-28,2003-10-29日分别变成了37922,37923,应该是1900年1月1日到两个日期的天数。我怎么把他们转变回去呢?希望大神们看清楚在回帖,不要答非所问。

[解决办法]

C/C++ code
#include <stdio.h>#include <time.h>void day2date(int days){    time_t time1;    struct tm* tminfo;    time(&time1);    tminfo = localtime(&time1);    tminfo->tm_isdst = 0;    tminfo->tm_year = 0;    tminfo->tm_mon = 0;    tminfo->tm_mday = days;    mktime(tminfo);    printf("Date: %04d-%02d-%02d\n", tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday);}int main(int argc, char* argv[]){    day2date(37922);    day2date(37923);    return 0;}
[解决办法]
简单写了一个:

C/C++ code
#include <stdio.h>#include <time.h>void GetDay(int days,int &month,int&mDay,bool bLeap){    int dayCount[12]={31,28,31,30,31,30,31,31,30,31,30,31};    if (bLeap)        dayCount[1]++;    for (int i = 0 ;i<12;i++)    {        if (days<=dayCount[i])        {            mDay = days;            return;        }        else        {            month++;            days-=dayCount[i];        }    }    return ;}bool isLeapYear(int year){    if (0==year%400)        return true;    if ((0==year%4) && (0!=year%100))        return true;    return false;}void day2date(int days){    int nYear = 1900;    int month =0;    int mDay =0;    int totalDays =365;    while (1)    {        if (isLeapYear(nYear))            totalDays=366;        else            totalDays=365;        if (days <= totalDays )            break;        days-=totalDays;        nYear++;    }    GetDay(days,month,mDay,isLeapYear(nYear));    printf("Date: %04d-%02d-%02d\n", nYear, month+1, mDay);}int main(int argc, char* argv[]){    day2date(37922);    day2date(37923);    getchar();    return 0;}
[解决办法]
探讨
为什么你的代码tm->hour = 8?
应该为0吧。

热点排行