c語言中有沒有能顯示系統日期和時間的函式

2021-12-19 14:27:52 字數 5742 閱讀 9857

1樓:蓴灬叔

c語言中讀取系統時間的函式為time(),其函式原型為:

#include

time_t time( time_t * ) ;

time_t就是long,函式返回從2023年1月1日(mfc是2023年12月31日)0時0分0秒,到現在的的秒數。可以呼叫ctime()函式進行時間轉換輸出:

char * ctime(const time_t *timer);

將日曆時間轉換成本地時間,按年月日格式,進行輸出,如:

wed sep 23 08:43:03 2015c語言還提供了將秒數轉換成相應的時間結構的函式:

struct tm * gmtime(const time_t *timer); //將日曆時間轉化為世界標準時間(即格林尼治時間)

struct tm * localtime(const time_t * timer); //將日曆時間轉化為本地時間

將通過time()函式返回的值,轉換成時間結構struct tm :

struct tm ;

程式設計者可以根據程式功能的情況,靈活的進行日期的讀取與輸出了。

例如:#include

main()

2樓:隆易植映寒

c語言中有沒有上述那種函式?如果有,直接給核心**,需給出標頭檔案和函式原型,如果沒有,做個那種功能的**,見**給分,可追加分數

3樓:匿名使用者

如果你是要用在windows系統的專案上,可以含入〈windows.h〉這個標頭檔案,取系統時間部分**如下:

systemtime time;

getlocaltime(&time);

time是個結構體,其中time.whour是小時,以此類推。函式原型我給不了。因為是用手機在回答。你可以搜尋該函式,百科上有詳盡介紹。

4樓:匿名使用者

//用標準c實現獲取當前系統時間的函式 一.time()函式time(&rawtime)函式獲取當前時間距2023年1月1日的秒數,以秒計數單位,存於rawtime 中。

#include "time.h"

void main ()

***************==

#include -- 必須的時間函式標頭檔案

time_t -- 時間型別(time.h 定義是typedef long time_t; 追根溯源,time_t是long)

struct tm -- 時間結構,time.h 定義如下:

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

time ( &rawtime ); -- 獲取時間,以秒計,從2023年1月一日起算,存於rawtime

localtime ( &rawtime ); -- 轉為當地時間,tm 時間結構

asctime ()-- 轉為標準ascii時間格式:

星期 月 日 時:分:秒 年

5樓:匿名使用者

有,這是我以前做的一個小例子。

#include

main()

c語言中 如何獲取系統時間

6樓:匿名使用者

方法一,#include

int main()

time_t timep;

struct tm *p;

time (&timep);

p=gmtime(&timep);

printf("%d\n",p->tm_sec); /*獲取當前秒*/

printf("%d\n",p->tm_min); /*獲取當前分*/

printf("%d\n",8+p->tm_hour);/*獲取當前時,這裡獲取西方的時間,剛好相差八個小時*/

printf("%d\n",p->tm_mday);/*獲取當前月份日數,範圍是1-31*/

printf("%d\n",1+p->tm_mon);/*獲取當前月份,範圍是0-11,所以要加1*/

printf("%d\n",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/

printf("%d\n",p->tm_yday); /*從今年1月1日算起至今的天數,範圍為0-365*/

方法二.#include

#include

int main ()

time_t t

獲取unix時間戳。

lt = localtime (&t);//轉為時間結構。

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

lt->tm_hour, lt->tm_min, lt->tm_sec);//輸出結果

return 0;}

擴充套件資料

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定義在sys\timeb.h,有四個fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( &timebuffer );

7樓:阿里

#include

int main()

time_t timep;

struct tm *p;

time (&timep);

p=gmtime(&timep);

printf("%d\n",p->tm_sec); /*獲取當前秒*/

printf("%d\n",p->tm_min); /*獲取當前分*/

printf("%d\n",8+p->tm_hour);/*獲取當前時,這裡獲取西方的時間,剛好相差八個小時*/

printf("%d\n",p->tm_mday);/*獲取當前月份日數,範圍是1-31*/

printf("%d\n",1+p->tm_mon);/*獲取當前月份,範圍是0-11,所以要加1*/

printf("%d\n",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/

printf("%d\n",p->tm_yday); /*從今年1月1日算起至今的天數,範圍為0-365*/

擴充套件連結

注意事項:

struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt->tm_year+1900。

8樓:兔丞飛

#include

#include

int main ()

time_t t

獲取unix時間戳。

lt = localtime (&t);//轉為時間結構。

printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//輸出結果

return 0;}

擴充套件資料

#include -- 必須的時間函式標頭檔案

time_t -- 時間型別(time.h 定義是typedef long time_t; 追根溯源,time_t是long)

struct tm -- 時間結構,time.h 定義如下:

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

time ( &rawtime ); -- 獲取時間,以秒計,從2023年1月一日起算,存於rawtime

localtime ( &rawtime ); -- 轉為當地時間,tm 時間結構

asctime ()-- 轉為標準ascii時間格式:

星期 月 日 時:分:秒 年

9樓:跪著作揖

獲取系統的時間需要藉助time()函式,具體的**如下:

#include

#include

struct mydate

unsigned year;

unsigned month;

unsigned day;

struct mydate today( )

struct mydate today;

time_t rawtime;

struct tm *timeinfo;

time ( &rawtime );

today.year = timeinfo->tm_year + 1900;

today.month = timeinfo->tm_mon + 1;

today.day = timeinfo->tm_mday;

return today;

int main( )

struct mydate today = today(  )

printf("%4d/%02d/%02d\n",today.year,today.month,today.day);

return 0;

擴充套件資料

#include

#include

int main (  )

time_t t;

struct tm * lt;

獲取unix時間戳。

轉為時間結構。

printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon,,lt->tm_mday,,lt->tm_hour, lt->tm_min,,lt->tm_sec);            //輸出結果

return 0;

c語言中有inta012而printfda為什麼輸出

int a 0 表示a是八進位制bai整數,所以du012就是八進位制12 即12q d是十zhi進dao制輸出,回那麼八轉十,12q 1x8 2 10d int a 025 025 q 21 d q表示八答進位制,d表示十進位制 int a 0xx 是表示八進位制,輸入時是顯示十進位制的,012轉...

c語言的相關運算,C語言中,有哪些算術運算子

後自增自減抄相當於這bai 樣一個du 函式x 可以這樣zhi理解 i x x x 1 i y x 10 第二題 dao i x x 1 i j x x 1 j 第一題bai因為1 0 而值為10 第二du題,答案為4無爭議。你說的zhi問題dao是編譯器專 的問題,例屬如 x x x 的問題,類似...

c語言乘方函式,C語言中沒有乘方符號嗎?

c語言的乘方運算可以利用庫函式pow。pow函式原型 double pow double x,doubley 標頭檔案 math.h cmath c 中 功能 計算x的y次冪。參考 include include int main 輸出 9 在c語言的標頭檔案 math.h中定義了pow x,y 返...