c++ 时间戳、日期格式、字符串

来源:互联网 发布:牛在印度的地位知乎 编辑:程序博客网 时间:2024/05/16 17:33
include<time.h>
time_t t;

t=time(0);//或者 time(&t);


time_t实际一般就是__int64,为时间戳


        char now[64];
        struct tm *ttime;
        ttime = localtime(&t);
        strftime(now,64,"%Y-%m-%d %H:%M:%S",ttime);
        cout << "the time is " << now << endl;

转换为字符串 : the time is 2008-05-30 15:29:59

char * ctime(const time_t *timer); // 

char *str_time = ctime(&t); // 转换成日历类型,包含星期几等信息
printf("str_time = %s\n", str_time);


更多参考:

http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html

原创粉丝点击