⑴ 用c語言如何獲取系統當前時間的函數

1、C語言中讀取系統時間的函數為time(),其函數原型為:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。
2、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 *變數來接收結果
3、常式:
#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}

時間結構體struct tm 說明:

struct tm {
int tm_sec; /* 秒 – 取值區間為[0,59] */
int tm_min; /* 分 - 取值區間為[0,59] */
int tm_hour; /* 時 - 取值區間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
int tm_year; /* 年份,其值等於實際年份減去1900 */
int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};

⑵ C語言如何獲取本地時間,然後取時、分、秒的值

C語言有2個獲取時間的函數,分別是time()和localtime(),time()函數返回unix時間戳-即從1970年1月1日0:00開始所經過得秒數,而localtime()函數則是將這個秒數轉化為當地的具體時間(年月日時分秒)

這里時間轉化要用到一個「struct tm*」的結構體,結構如下:

struct tm {

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

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

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

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

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

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

int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一 */

int tm_yday; /* 從每年1月1日開始的天數– 取值區間[0,365],其中0代表1月1日 */

int tm_isdst; /* 夏令時標識符,夏令時tm_isdst為正;不實行夏令時tm_isdst為0 */

};

示例代碼

#include<stdio.h>

#include<time.h>

int getTime()

{

time_t t; //保存unix時間戳的變數 ,長整型

struct tm* lt; //保存當地具體時間的變數

int p;

time(&t); // 等價於 t =time(NULL);獲取時間戳

lt = localtime(&t); //轉化為當地時間

p = lt->tm_sec; //將秒數賦值給p

return p;

}

應該就是這樣啦~

⑶ C語言中如何獲取當前系統時間的小時

程序主要通過當前系統日歷的struct tm結構體獲得,主要代碼如下,
#include <stdio.h>
#include <time.h>
//程序功能輸出當前時間在24H下的小時數
int main(int argc, char *argv[])
{
struct tm *ptr;
time_t lt;
time(<);//當前系統時間
ptr=localtime(<);//獲取本地日歷時間指針
printf("hour=%d(24H )\n",ptr->tm_hour);//輸出24H下的小時數
return 0;
}

結構體tm定義如下,
struct tm {
int tm_sec; /* 秒–取值區間為[0,59] */
int tm_min; /* 分 - 取值區間為[0,59] */
int tm_hour; /* 時 - 取值區間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
int tm_year; /* 年份,其值從1900開始 */
int tm_wday; /* 星期–取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數–取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
long int tm_gmtoff; /*指定了日期變更線東面時區中UTC東部時區正秒數或UTC西部時區的負秒數*/
const char *tm_zone; /*當前時區的名字(與環境變數TZ有關)*/
};

⑷ 如何用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語言中 如何獲取系統時間

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

(5)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語言中如何獲取當前日期

#include
#include
void
main(){
struct
tm
*
tmptr;//時間的結構體
time_t
secnow;
time(&secnow);
tmptr
=
localtime(&secnow);
int
hour1,min1;
hour1
=
tmptr->tm_hour;
printf("the
time
is
%02d",hour1);//輸出當前小時-----------這才是人版家提問的要的時間的數字權!
}

⑺ c語言中取系統時間

主要分為兩種方法:

1.這種方法比較高級

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

#include<time.h>
intmain(intargc,char**argv)
{
_ttemp;
structtm*t;
time(&temp);
t=localtime(&temp);

printf("當前時間是: %d年%d月%d日 ",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
printf("%d時%d分%d秒 ",t->tm_hour,t->tm_min,t->tm_sec);
/*
t結構體內的成員變數還有以下幾個:
tm_wday 星期的第幾天 tm_yday 這天是這年的第幾天
*/
return0;
}

需要注意的是tm_year返回的是1900年之後的年數,tm_mon返回的比實際月份小1(至於為什麼要這樣設計,我不是太清楚)

2.這種方法較為簡單方便,但是同時可能會對接下來的其它操作不利。

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

intmain(intargc,char**argv)
{
time_ttemp;
time(&temp);
printf("當前時間為: %s",ctime(&temp));
return0;
}

⑻ C語言中 如何獲取系統時間

#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;}
(8)c語言取當前時間擴展閱讀
#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
);
--
獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime
(
&rawtime
);
--
轉為當地時間,tm
時間結構
asctime
()--
轉為標准ASCII時間格式:
星期


時:分:秒

參考資料:網路
time函數