#include <time.h>
// time_t是long类型,精确到秒。获取自1970年1月1日以来持续时间的秒数
const time_t t = time(NULL);
/*本地时间:日期,时间 年月日,星期,时分秒*/
struct tm* current_time = localtime(&t);
printf("current year is %d;current month is %d;current date of month is %d\r\n",
1900 + current_time->tm_year,
1 + current_time->tm_mon/*此month的范围为0-11*/,
current_time->tm_mday);
// 将时间转换为自1970年1月1日以来持续时间的秒数,发生错误时返回-1
time_t mktime(struct tm * timeptr);
//strftime
#include"time.h"
#include"stdio.h"
int main(void){
struct tm* ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(<);
strftime(str,sizeof(str),"Itisnow%I%p",ptr);
printf("%s\n",str);
return0;
}
#include<stdio.h>
#include<time.h>
int main(void)
{
time_ttimep;
structtm*p;
time(&timep);
printf("time():%d\n",timep);
p=localtime(&timep);
timep=mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
return0;
}