手机版
你好,游客 登录 注册
背景:
阅读新闻

C语言中获得系统当前日期 和 时间

[日期:2012-07-12] 来源:Linux社区  作者:shylx123 [字体:大 中 小]

C语言中获得系统当前的日期和时间

1. 

  1. #include <stdio.h>   
  2. #include <time.h>   
  3. int main()  
  4. {  
  5.     time_t timep;  
  6.     struct tm *p;  
  7.     time(&timep);  
  8.     p =localltime(&timep); //此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间   
  9.     //p = gmtime(&timep); //把日期和时间转换为格林威治(GMT)时间的函数   
  10.   
  11.     printf("Year:  %d\n", 1900+p->tm_year);  
  12.     printf("Month:  %d\n", 1+p->tm_mon);  
  13.     printf("Day:  %d\n", p->tm_mday);  
  14.     printf("Hour:  %d\n", p->tm_hour);  
  15.     printf("Minute:  %d\n", p->tm_min);  
  16.     printf("Second:  %d\n",  p->tm_sec);  
  17.     printf("Weekday:  %d\n", p->tm_wday);  
  18.     printf("Days:  %d\n", p->tm_yday);  
  19.     printf("Isdst:  %d\n", p->tm_isdst);  
  20. } 

2. 

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  Time04.c 
  5.  * 
  6.  *    Description:  输入格式化后的系统当前时间 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  2012年07月10日 22时34分31秒 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  ShanHaiyang  
  14.  *   Organization:   
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18. #include <stdlib.h>   
  19. #include <stdio.h>   
  20. #include <time.h>   
  21. #include <string.h>   
  22.   
  23. int main()  
  24. {  
  25.     time_t timep;  
  26.     char s[30];  
  27.       
  28.     time(&timep);  
  29.   
  30.     strcpy(s,ctime(&timep));  
  31.   
  32.     printf("%s\n", s);  
  33. }  
linux
相关资讯       C语言 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款