如何用c語言獲取當前系統時間,如何用C語言獲取當前系統時間?

時間 2021-12-19 11:39:03

1樓:風若遠去何人留

需要利用c語言的時間函式time和localtime,具體說明如下:

一、函式介面介紹:

1、time函式。

形式為time_t time (time_t *__timer);

其中time_t為time.h定義的結構體,一般為長整型。

這個函式會獲取當前時間,並返回。 如果引數__timer非空,會儲存相同值到__timer指向的記憶體中。

time函式返回的為unix時間戳,即從2023年1月1日(utc/gmt的午夜)開始所經過的秒數,不考慮閏秒。

由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函式了。

2、localtime函式。

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

其中tm為一個結構體,包含了年月日時分秒等資訊。

這種結構是適合用來輸出的。

#include

#include

int main ()

注意事項:

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

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

2樓:匿名使用者

方法一,#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 );

3樓:阿里

#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。

4樓:兔丞飛

#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時間格式:

星期 月 日 時:分:秒 年

5樓:跪著作揖

獲取系統的時間需要藉助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;

6樓:風若遠去何人留

需要利用c語言的時間函式time和localtime,具體說明如下:

一、函式介面介紹:

1、time函式。

形式為time_t time (time_t *__timer);

其中time_t為time.h定義的結構體,一般為長整型。

這個函式會獲取當前時間,並返回。 如果引數__timer非空,會儲存相同值到__timer指向的記憶體中。

time函式返回的為unix時間戳,即從2023年1月1日(utc/gmt的午夜)開始所經過的秒數,不考慮閏秒。

由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函式了。

2、localtime函式。

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

其中tm為一個結構體,包含了年月日時分秒等資訊。

這種結構是適合用來輸出的。

#include

#include

int main ()

注意事項:

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

7樓:匿名使用者

1.首先是在dev c++中新建c語言的程式,如下圖所示2.接下來在c語言檔案中新增main函式,如下圖所示3.

然後在頭部的庫檔案中匯入time.h庫檔案,如下圖所示4.接下來通過庫檔案中的gmtime獲取當前時間,如下圖所示5.

然後我們就可以通過如下圖所示的方式分別獲取時間中的年月日等資訊6.最後我們執行c程式,就可以得到系統時間並獲得時間中的年月日7.綜上所述,在c語言中獲得系統時間一定要先引入time.

h庫,大家快去試一下吧

C 中如何獲取當前時間

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include include int main void size t strftime char strdest,size t maxsize,const char format,const struct tm timeptr 根據格式字串生成字...

如何獲取當前系統時間,如何獲取前一天的系統日期

通過php獲取系統前一天的日期,用到的工具,notepad 步驟如下 通過notepad 寫一個php函式另存為檔案。說明 的含義,就是先通過strtotime將當前時間減去一天轉化成unix時間戳,然後data函式是將unix時間戳轉化成年月日時分秒。2.瀏覽器中執行函式,輸出結果如下 注意事項 ...

通過Calendar獲取系統的當前時間後,我想把時分秒都的值都設成0,分秒成功啦,但是小時卻不可以,求高手

那就先把時間設定成明天,再設成0點 cal.add calendar.date,1 cal.set calendar.hour,0 cal.set calendar.hour of day,0 這一項直接改為 cal.set calendar.hour,0 calendar.set calendar...