欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

c語言獲取日期的函數(shù) c語言獲取時間

C語言中有沒有能顯示系統(tǒng)日期和時間的函數(shù)?

C語言中讀取系統(tǒng)時間的函數(shù)為time(),其函數(shù)原型為:

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計制作、成都做網(wǎng)站與策劃設計,磴口網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:磴口等地區(qū)。磴口做網(wǎng)站價格咨詢:028-86922220

#include time.h

time_t time( time_t * ) ;

time_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現(xiàn)在的的秒數(shù)。可以調用ctime()函數(shù)進行時間轉換輸出:

char * ctime(const time_t *timer);

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

Wed Sep 23 08:43:03 2015

C語言還提供了將秒數(shù)轉換成相應的時間結構的函數(shù):

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

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

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

struct tm {

int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */

int tm_min; /* 分 - 取值區(qū)間為[0,59] */

int tm_hour; /* 時 - 取值區(qū)間為[0,23] */

int tm_mday; /* 一個月中的日期 - 取值區(qū)間為[1,31] */

int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */

int tm_year; /* 年份,其值等于實際年份減去1900 */

int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */

int tm_yday; /* 從每年的1月1日開始的天數(shù) – 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */

int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/

};

編程者可以根據(jù)程序功能的情況,靈活的進行日期的讀取與輸出了。

例如:

#includetime.h

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);/*獲取當前月份日數(shù),范圍是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日算起至今的天數(shù),范圍為0-365*/

}

在c語言中如何使用系統(tǒng)函數(shù)得到當前的日期?

獲得日期和時間

這里說的日期和時間就是我們平時所說的年、月、日、時、分、秒等信息。從第2節(jié)我們已經知道這些信息都保存在一個名為tm的結構體中,那么如何將一個日歷時間保存為一個tm結構的對象呢?

其中可以使用的函數(shù)是gmtime()和localtime(),這兩個函數(shù)的原型為:

struct

tm

*

gmtime(const

time_t

*timer);

struct

tm

*

localtime(const

time_t

*

timer);

其中gmtime()函數(shù)是將日歷時間轉化為世界標準時間(即格林尼治時間),并返回一個tm結構體來保存這個時間,而localtime()函數(shù)

是將日歷時間轉化為本地時間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標準時間是2005年7月30日7點18分20秒,那么我用

localtime()函數(shù)在中國地區(qū)獲得的本地時間會比世界標準時間晚8個小時,即2005年7月30日15點18分20秒。下面是個例子:

#include

"time.h"

#include

"stdio.h"

int

main(void)

{

struct

tm

*local;

time_t

t;

t=time(NUL);

local=localtime(t);

printf("Local

hour

is:

%d\n",local-tm_hour);

local=gmtime(t);

printf("UTC

hour

is:

%d\n",local-tm_hour);

return

0;

}

運行結果是:

Local

hour

is:

15

UTC

hour

is:

7

固定的時間格式

我們可以通過asctime()函數(shù)和ctime()函數(shù)將時間以固定的格式顯示出來,兩者的返回值都是char*型的字符串。返回的時間格式為:

星期幾

月份

日期

時:分:秒

年\n{post.content}

例如:Wed

Jan

02

02:03:55

1980\n{post.content}

其中\(zhòng)n是一個換行符,{post.content}是一個空字符,表示字符串結束。下面是兩個函數(shù)的原型:

Char

*

asctime(const

struct

tm

*

timeptr);

char

*

ctime(const

time_t

*timer);

其中asctime()函數(shù)是通過tm結構來生成具有固定格式的保存時間信息的字符串,而ctime()是通過日歷時間來生成時間字符串。這樣的

話,asctime()函數(shù)只是把tm結構對象中的各個域填到時間字符串的相應位置就行了,而ctime()函數(shù)需要先參照本地的時間設置,把日歷時間轉

化為本地時間,然后再生成格式化后的字符串。在下面,如果t是一個非空的time_t變量的話,那么:

printf(ctime(t));

等價于:

struct

tm

*ptr;

ptr=localtime(t);

printf(asctime(ptr));

那么,下面這個程序的兩條printf語句輸出的結果就是不同的了(除非你將本地時區(qū)設為世界標準時間所在的時區(qū)):

#include

"time.h"

#include

"stdio.h"

int

main(void)

{

struct

tm

*ptr;

time_t

lt;

lt

=time(NUL);

ptr=gmtime();

printf(asctime(ptr));

printf(ctime());

return

0;

}

運行結果:

Sat

Jul

30

08:43:03

2005

Sat

Jul

30

16:43:03

2005

用c語言如何獲取系統(tǒng)當前時間的函數(shù)?

方法一,#includetime.h

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);/*獲取當前月份日數(shù),范圍是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日算起至今的天數(shù),范圍為0-365*/

}

方法二.#include?stdio.h

#include?time.h

int?main?()

{

time_t?t

struct?tm?*?lt;????time?(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; //計算當前系統(tǒng)時間與時間t1的間隔

int iDay=span.GetDays(); //獲取這段時間間隔共有多少天

int iHour=span.GetTotalHours(); //獲取總共有多少小時

int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘

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

2、timeb()函數(shù)

_timeb定義在SYS\TIMEB.H,有四個fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( timebuffer );

參考資料來源:百度百科:time函數(shù)

標題名稱:c語言獲取日期的函數(shù) c語言獲取時間
網(wǎng)頁URL:http://chinadenli.net/article12/hghidc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管商城網(wǎng)站網(wǎng)站設計公司云服務器定制開發(fā)品牌網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化