当前位置: 代码迷 >> 综合 >> C库(time.h)函数——time() localtime() asctime()
  详细解决方案

C库(time.h)函数——time() localtime() asctime()

热度:92   发布时间:2024-02-23 06:43:17.0
  • time()

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char *argv[]) 
{//分别声明两种使用方式的赋值对象time_t t1,t2,t3; t1=time(0);     //第一种方式 t2=time(NULL);    //第一种方式 time(&t3);    //第二种使用方式printf("t1=%ld\n",t1);printf("t2=%ld\n\n",t2);printf("t3=%ld\n",t3);return 0;
}

 time()函数返回一个值,即格林尼治时间1970年1月1日00:00:00到当前时刻的秒数

  • asctime()

 

#include <stdio.h>
#include <string.h>
#include <time.h>int main(int argc ,char *argv[])
{struct tm t;t.tm_sec    = 10;     // 秒,范围从 0 到 59  t.tm_min    = 10;     //分,范围从 0 到 59  t.tm_hour   = 6;      //  小时,范围从 0 到 23 t.tm_mday   = 27;	//     一月中的第几天,范围从 1 到 31t.tm_mon    = 8;		//    月份,范围从 0 到 11t.tm_year   = 120;    //   自 1900 起的年数t.tm_wday   = 6;      //    一周中的第几天,范围从 0 到 6puts(asctime(&t));     //asctime() 函数返回一个字符串,例如"Sat Sep 27 06:10:10 2020"return(0);
}
  • localtime()
#include <stdio.h>
#include <time.h>int main (int argc,char *argvv[])
{time_t rawtime;struct tm *info;char buffer[80];time( &rawtime );info = localtime( &rawtime );     //printf("当前的本地时间和日期:%s", asctime(info));return(0);
}

 

  相关解决方案