C语言: 按照格式返回系统时间的的函数

原文地址:http://blog.csdn.net/hnwanghb/article/details/319837

/*
功能描述:获取当前的系统时间格式YYYYMMDDhhmmss,精确到秒 
参数描述:char * curtime传地址方式传入的参数,用于返回指定格式的时间字符串,
          int flag: 1-将时间做成YYYYMMDDhhmmss格式  2-将时间做成YYYY-MM-DD hh:mm:ss格式  3-将时间做成YYYYMMDD格式
返回参数:无 (格式为YYYYMMDDhhmmss或者为YYYY-MM-DD hh:mm:ss的字符串)
*/

void get_curtime(int flag,char * timeformat)
{
   char outstr[128];
   char str[100];
   
   time_t t; 
   struct tm *gmt, *area;

   t = time(NULL); 
   area = localtime(&t); 
   
   memset(outstr,0x0,sizeof(outstr));
   
   if(flag==1)
   {
    sprintf(str,"%4d",1900+area->tm_year);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_mon+1);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_mday);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_hour);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_min);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_sec);
        strcat(outstr,str);
   }
   else if(flag==3)
   {
        sprintf(str,"%4d",1900+area->tm_year);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_mon+1);
    strcat(outstr,str);
    sprintf(str,"%02d ",area->tm_mday);
    strcat(outstr,str);    
   }
   else
   {
    sprintf(str,"%4d-",1900+area->tm_year);
    strcat(outstr,str);
    sprintf(str,"%02d-",area->tm_mon+1);
    strcat(outstr,str);
    sprintf(str,"%02d ",area->tm_mday);
    strcat(outstr,str);
    sprintf(str,"%02d:",area->tm_hour);
    strcat(outstr,str);
    sprintf(str,"%02d:",area->tm_min);
    strcat(outstr,str);
    sprintf(str,"%02d",area->tm_sec); 
        strcat(outstr,str);
   }
   strcpy(timeformat,outstr);
}


添加:

void main()

{

      char Time_char[128];

      get_curtime(2,time_char);

      printf("\007The current date/time is: %s\n", time_char);//获取YYYY-MM-DD hh:mi:ss格式时间

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值