当前位置:首页 » 编程语言 » c语言显示系统时间

c语言显示系统时间

发布时间: 2024-05-28 13:41:52

Ⅰ 如何用c语言获取当前系统时间

需要利用C语言的时间函数time和localtime,具体说明如下:

一、函数接口介绍:

1、time函数。

形式为time_t time (time_t *__timer);

其中time_t为time.h定义的结构体,一般为长整型。

这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。

time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。

2、localtime函数。

形式为struct tm *localtime (const time_t *__timer);

其中tm为一个结构体,包含了年月日时分秒等信息。

这种结构是适合用来输出的。

二、参考代码:

#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//获取Unix时间戳。
lt=localtime(&t);//转为时间结构。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果
return0;
}

注意事项:

struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt->tm_year+1900。

Ⅱ C语言怎样获取系统当前的时间并把它保存到定义的变量中

C语言中读取系统时间的函数为time(),其函数原型为:

#include <time.h>

time_t time( time_t * ) ;

time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。

C语言还提供了将秒数转换成相应的时间格式的函数:

  • char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值

  • struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果

  • struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果

例:

#include<time.h>
voidmain()
{
time_tt;
structtm*pt;
char*pc;
time(&t);
pc=ctime(&t);printf("ctime:%s",pc);
pt=localtime(&t);printf("year=%d",pt->tm_year+1900);
}

时间结构体struct tm 说明:

structtm{
inttm_sec;/*秒–取值区间为[0,59]*/
inttm_min;/*分-取值区间为[0,59]*/
inttm_hour;/*时-取值区间为[0,23]*/
inttm_mday;/*一个月中的日期-取值区间为[1,31]*/
inttm_mon;/*月份(从一月开始,0代表一月)-取值区间为[0,11]*/
inttm_year;/*年份,其值等于实际年份减去1900*/
inttm_wday;/*星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推*/
inttm_yday;/*从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推*/
inttm_isdst;/*夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};

Ⅲ 如何用C语言编写一个显示时间的函数,要求时间显示精度到毫秒级别。


#include <cstdio>

#include <ctime>

using namespace std;


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void printTime() {

struct tm t; //tm结构指针

time_t now; //声明time_t类型变量

time(&now); //获取系统日期和时间

localtime_s(&t, &now); //获取当地日期和时间


//格式化输出本地时间

printf("年-月-日-时-分-秒:%d-%d-%d %d:%d:%d ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);


}


int main(int argc, char** argv) {

printTime();

}

Ⅳ 镐庢牱鐢–璇瑷鏄剧ず绯荤粺镞堕棿骞惰兘璁$畻镞堕棿宸

//---------------------------------------------------------------------------

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

int main(void)
{
time_t b,e;
char timestr[80];
b=time(NULL);
strftime(timestr,79,"%H:%M:%S",localtime(&b));
puts(timestr); /*杈揿嚭褰揿墠镞跺埢*/
puts("press ENTER..."); /*绛夊緟鎸夊洖杞﹂敭*/
getchar();
e=time(NULL);
strftime(timestr,79,"%H:%M:%S",localtime(&e));
puts(timestr); /*杈揿嚭褰揿墠镞跺埢*/
printf("%d Sec.\n",e-b); /*杈揿嚭镞舵椂闂撮殧*/
return 0;
}
//---------------------------------------------------------------------------

Ⅳ C语言显示系统时间

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

time_t//时间类型(time.h定义)
structtm{//时间结构,time.h定义如下:
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
}
time(&rawtime);//获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime(&rawtime);//转为当地时间,tm时间结构
asctime()//转为标准ASCII时间格式:
//就是直接打印tm,tm_year从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

Ⅵ C语言中 如何获取系统时间

方法一,#include<time.h>

int main()

{

time_t timep;

struct tm *p;

time (&timep);

p=gmtime(&timep);

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

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

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

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

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

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

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

}

方法二.#include<stdio.h>

#include<time.h>

intmain()

{

time_tt

structtm*lt;time(&t);//获取Unix时间戳。

lt=localtime(&t);//转为时间结构。

printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,

lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果

return0;}

(6)c语言显示系统时间扩展阅读

1、CTimeSpan类

如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

CTime t1( 1999, 3, 19, 22, 15, 0 );

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔

int iDay=span.GetDays(); //获取这段时间间隔共有多少天

int iHour=span.GetTotalHours(); //获取总共有多少小时

int iMin=span.GetTotalMinutes();//获取总共有多少分钟

int iSec=span.GetTotalSeconds();//获取总共有多少秒

2、timeb()函数

_timeb定义在SYSTIMEB.H,有四个fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( &timebuffer );

Ⅶ C语言中怎样调用系统时间并动态显示!

得到系统时间:
1.使用CTime类

CTime tm=CTime::GetCurrentTime();

CString str=tm.Format(“现在时间是:%Y年%m月%d日 %X”);

MessageBox(str,NULL,MB_OK);

2: 得到系统时间日期(使用GetLocalTime)

SYSTEMTIME st;

CString strDate,strTime;

GetLocalTime(&st);

strDate.Format(“%4d-%2d-%2d”,st.wYear,st.wMonth,st.wDay);

strTime.Format(“%2d:%2d:%2d”,st.wHour,st.wMinute,st.wSecond);

3.使用GetTickCount//获取程序运返和行时间

long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)

……//程序段

long t2=GetTickCount();//程序段睁猛结束后取得系统运行时间(ms)

long t = t2-t1; //前后之差悉世桥即 程序运行时间 (ms)

热点内容
db2新建数据库 发布:2024-09-08 08:10:19 浏览:170
频率计源码 发布:2024-09-08 07:40:26 浏览:778
奥迪a6哪个配置带后排加热 发布:2024-09-08 07:06:32 浏览:100
linux修改apache端口 发布:2024-09-08 07:05:49 浏览:208
有多少个不同的密码子 发布:2024-09-08 07:00:46 浏览:566
linux搭建mysql服务器配置 发布:2024-09-08 06:50:02 浏览:995
加上www不能访问 发布:2024-09-08 06:39:52 浏览:811
银行支付密码器怎么用 发布:2024-09-08 06:39:52 浏览:513
苹果手机清理浏览器缓存怎么清理缓存 发布:2024-09-08 06:31:32 浏览:554
云服务器的优点与缺点 发布:2024-09-08 06:30:34 浏览:734