當前位置:首頁 » 編程語言 » 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)

熱點內容
數據模型於資料庫系統 發布:2024-07-27 09:59:56 瀏覽:849
網站ip訪問量 發布:2024-07-27 09:59:55 瀏覽:286
java字元串字元數組 發布:2024-07-27 09:59:13 瀏覽:593
如何利用無線網路搭建伺服器 發布:2024-07-27 09:59:05 瀏覽:721
it天空解壓密碼 發布:2024-07-27 09:50:39 瀏覽:549
軟體腳本買賣 發布:2024-07-27 09:50:38 瀏覽:917
android對象轉json 發布:2024-07-27 09:50:15 瀏覽:183
安卓平板有什麼可以畫對稱的 發布:2024-07-27 09:36:03 瀏覽:133
羊創意腳本 發布:2024-07-27 09:29:30 瀏覽:895
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:486