一个整数日期时间连接的格式化处理函数

来源:互联网 发布:中山大学网络教育平台 编辑:程序博客网 时间:2024/05/21 07:39

因为用到,写了这个函数,先记在这,可能还用的上

int ConvertIntToDateTimeString(int nDate,int nTime,char * strDateTime)
{
        char strBuff[25];
        if(nDate>0 && nTime>0)
        {
                itoa(nDate,strBuff,10);
                if(strlen(strBuff)!=8)
                        return 0;
                itoa(nTime,strBuff,10);
                if(strlen(strBuff)!=6)
                        return 0;
               
                int nYear=0,nMonth=0,nDay=0,
                        nHour=0,nMin=0,nSec=0;
                nYear=nDate;
                nHour=nTime;
                nDay=nYear%100;
                nYear/=100;
                nMonth=nYear%100;
                nYear/=100;
               
                nSec=nHour%100;
                nHour/=100;
                nMin=nHour%100;
                nHour/=100;
               
                memset(strBuff,0,sizeof(strBuff));
                sprintf(strBuff,"%04d-%02d-%02d %02d:%02d:%02d",nYear,nMonth,nDay,nHour,nMin,nSec);
               
                if( nYear >=1900 && nYear  <=9999 &&
                        nMonth>=1    && nMonth <=12   &&
                        nDay  >=1    && nDay   <=31   &&
                        nHour >=0    && nHour  <=23   &&
                        nMin  >=0    && nMin   <=60   &&
                        nSec  >=0    && nSec   <=60 )
                {
                        strcpy(strDateTime,strBuff);
                        return 1;
                }
        }
        return 0;
}

调用示例:
char strRet[25]={0};
ConvertIntToDateTimeString(20110309,150225,strRet);

 

 

 

 

原创粉丝点击