时间戳转格式化日期,日期转换成时间戳

1.时间戳转格式化日期,比如:1384936600 → 2013-11-20 08:36:40  输入一个long,输出一个nsstring

2.反过来:2013-11-20 08:36:40 → 1384936600 输入nsstring,输出一个long

好久没碰c语言。。好多函数都现查怎么用,还好一会就搞定了

1.时间戳转格式化日期

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.     time_t t;  
  7.     struct tm *p;  
  8.     t=1384936600;  
  9.     p=gmtime(&t);  
  10.     char s[100];  
  11.     strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);  
  12.     printf("%d: %s\n", (int)t, s);  
  13.     return 0;  
  14. }  

2.格式化日期转时间戳

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.     struct tm* tmp_time = (struct tm*)malloc(sizeof(struct tm));  
  7.     strptime("20131120","%Y%m%d",tmp_time);  
  8.     time_t t = mktime(tmp_time);  
  9.     printf("%ld\n",t);  
  10.     free(tmp_time);  
  11.     return 0;  
  12. }  

猜你喜欢

转载自blog.csdn.net/weibo1230123/article/details/80277243