其他分享
首页 > 其他分享> > 使用c语言获取时间戳,当前时间

使用c语言获取时间戳,当前时间

作者:互联网

主要使用到的是C语言中读取系统时间的函数——time()。

 

#include <time.h>
#include <stdio.h>

int main()

{
char buffer [128];

time_t timep;

struct tm *p;

time (&timep);

p=gmtime(&timep);

printf("年:%d\n",1900+p->tm_year);/*获取当前年份,从1900开始,所以要加1900*/

printf("月:%d\n",1+p->tm_mon);/*获取当前月份,范围是0-11,所以要加1*/

printf("日:%d\n",p->tm_mday);/*获取当前月份日数,范围是1-31*/

printf("时::%d\n",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/

printf("分:%d\n",p->tm_min); /*获取当前分*/

printf("秒:%d\n",p->tm_sec); /*获取当前秒*/

printf("今年第:%d天\n",p->tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/

strftime (buffer,sizeof(buffer),"Now is %Y/%m/%d %H:%M:%S",p);

printf("%s\n", buffer);

}

 

标签:语言,buffer,获取,tm,时间,当前,printf,1900
来源: https://www.cnblogs.com/dzswise/p/15464174.html