现在的位置: 首页 > 综合 > 正文

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

2014年01月09日 ⁄ 综合 ⁄ 共 1442字 ⁄ 字号 评论关闭

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

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);

 

 

 

 

抱歉!评论已关闭.