當前位置:首頁 » 編程語言 » c語言獲得系統時間代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言獲得系統時間代碼

發布時間: 2022-09-20 11:15:04

❶ 用c語言獲取時間

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("當前系統時間:%s",asctime(timeinfo));
return0;
}

說明:
time_t // 時間類型(time.h 定義)
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時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

❷ 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語言如何獲取系統當前時間的函數

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

#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*/

}

拓展資料:

C語言是一門通用計算機編程語言,廣泛應用於底層開發。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。

盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。

二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。

C語言是一門面向過程的計算機編程語言,與C++,Java等面向對象的編程語言有所不同。

其編譯器主要有Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C等。

❺ c語言中獲取當前時間的代碼,求解釋!!

(1)

time_ttime(time_t*timer);

The function returns this value, and if the argument is not a null pointer,
the value is also set to the object pointed by timer.

(2)

structtm*localtime(consttime_t*timer);

Uses the time pointed by timer to fill a tm
structure with the values that represent the corresponding local time.

(3)

structtm{
inttm_sec;/*secondsaftertheminute-[0,59]*/
inttm_min;/*minutesafterthehour-[0,59]*/
inttm_hour;/*hourssincemidnight-[0,23]*/
inttm_mday;/*dayofthemonth-[1,31]*/
inttm_mon;/*monthssinceJanuary-[0,11]*/
inttm_year;/*yearssince1900*/
inttm_wday;/*dayssinceSunday-[0,6]*/
inttm_yday;/*dayssinceJanuary1-[0,365]*/
inttm_isdst;/*daylightsavingstimeflag*/
};

( 4 )

intatoi(constchar*str);

Convert a string to integer.

你代碼中的if語句就是拿當前時間records中第i個時間比較,如果當前時間的小時不大於records[i]的小時,且分鍾小於records[i]中的分鍾,則返回1(應該是沒超出),否則返回0(超出)。

懂了嗎,寶貝?

❻ 看過來,看過來 C語言獲取系統時間的幾種方式

我們在寫C語言程序的時候,有的時候會用到讀取本機的時間和日期,怎麼做呢?其實很簡單的,下面簡單說一下:

C語言中讀取系統時間的函數為time(),其函數原型為:#include <time.h>time_t time( time_t * ) ;

time_t就是long,函數返回從1970年1月1日(MFC是1899年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()函數返回的值,轉成時間結構structtm :

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()為負。*/};

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

下面給出一段簡單的代碼:

#include<time.h>
intmain()
{
time_ttimep;
structtm*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*/
}

❼ 標准C語言獲取系統時間

1、代碼如下:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#include <time.h>

void main()
{
time_t t = time(NULL);
tm *tmTemp=localtime(&t);
printf("%04d-%02d-%02d %02d:%02d:%02d", tmTemp->tm_year+1900,tmTemp->tm_mon+1,tmTemp->tm_mday ,tmTemp->tm_hour,tmTemp->tm_min,tmTemp->tm_sec);

system("PAUSE");
}

2、不知道你說的"選擇在兩個時間中間的所有信息"是什麼意思?

❽ c語言中取系統時間

主要分為兩種方法:

1.這種方法比較高級

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

#include<time.h>
intmain(intargc,char**argv)
{
time_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<time.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);/*獲取當前月份日數,范圍是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 <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;}
(9)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函數

❿ C語言獲取系統時間

#include <stdio.h>
#include <time.h>
void main ()
{ time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include <time.h> -- 必須的時間函數頭文件
time_t -- 時間類型(time.h 定義)
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時間格式: 星期 月 日 時:分:秒 年 =========================================
你要的格式可這樣輸出: printf ( "M-d-d d:d:d\n",1900+timeinfo->tm_year, 1+timeinfo->tm_mon, timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec); 就是直接列印tm,tm_year 從1900年計算,所以要加1900, 月tm_mon,從0計算,所以要加1 其它你一目瞭然啦。