Ⅰ c語言功能:獲取當前日期時間
#include
#include
int
main()
{
time_t
timep;
struct
tm
*p;
time(&timep);
p
=
localtime(&timep);
//此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
printf("%d%02d%02d%02d%02d%02d\n",
1900+p->tm_year,
1+p->tm_mon,
p->tm_mday,
p->tm_hour,
p->tm_min,
p->tm_sec);
return
0;
}
Ⅱ 問在C語言里怎麼獲取當前時間和日期
#include <time.h> 要添加這個頭文件。
time_t rawtime;
struct tm * target_time;
time ( &rawtime ); //獲取當前時間,存rawtime里
target_time = localtime ( &rawtime ); //獲取當地時間
利用struct tm,你可以按需取出年月日時分秒星期幾等數值。
---------------------
你的問題:
time_t now;
long int dt=3600; // 時間長度,秒數
now = time (NULL); //獲取當前時間
printf("%s ",ctime(&now)); //直接列印時間
now=now+dt;
printf("%s ",ctime(&now)); // 直接列印加dt後的時間
(當然,你也可以用 ctime(&now) 返回的字元串 通過 MFC 的方法顯示)
Ⅲ C語言如何獲取本地時間,然後取時、分、秒的值
#include <stdio.h>
#include <time.h>
int main()
{time_t timep;
struct tm *tp;
time(&timep);
int p;
tp = localtime(&timep); //取得系統時間
printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);
printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);
p=tp->tm_sec;
printf("p=%d ",p);
return 0;
}
Ⅳ 用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
#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語言獲取時間
#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語言如何輸出當前的日期和時間
#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;//time_t是一種類型,定義time_t類型的t
time(&t);//取得當前時間
printf("%s ",ctime(&t));//ctime(&t)將日期轉為字元串並列印
return0;
}
Ⅷ 在c語言中如何使用系統函數得到當前的日期
獲得日期和時間
這里說的日期和時間就是我們平時所說的年、月、日、時、分、秒等信息。從第2節我們已經知道這些信息都保存在一個名為tm的結構體中,那麼如何將一個日歷時間保存為一個tm結構的對象呢?
其中可以使用的函數是gmtime()和localtime(),這兩個函數的原型為:
struct
tm
*
gmtime(const
time_t
*timer);
struct
tm
*
localtime(const
time_t
*
timer);
其中gmtime()函數是將日歷時間轉化為世界標准時間(即格林尼治時間),並返回一個tm結構體來保存這個時間,而localtime()函數
是將日歷時間轉化為本地時間。比如現在用gmtime()函數獲得的世界標准時間是2005年7月30日7點18分20秒,那麼我用
localtime()函數在中國地區獲得的本地時間會比世界標准時間晚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()函數和ctime()函數將時間以固定的格式顯示出來,兩者的返回值都是char*型的字元串。返回的時間格式為:
星期幾
月份
日期
時:分:秒
年\n{post.content}
例如:Wed
Jan
02
02:03:55
1980\n{post.content}
其中\n是一個換行符,{post.content}是一個空字元,表示字元串結束。下面是兩個函數的原型:
Char
*
asctime(const
struct
tm
*
timeptr);
char
*
ctime(const
time_t
*timer);
其中asctime()函數是通過tm結構來生成具有固定格式的保存時間信息的字元串,而ctime()是通過日歷時間來生成時間字元串。這樣的
話,asctime()函數只是把tm結構對象中的各個域填到時間字元串的相應位置就行了,而ctime()函數需要先參照本地的時間設置,把日歷時間轉
化為本地時間,然後再生成格式化後的字元串。在下面,如果t是一個非空的time_t變數的話,那麼:
printf(ctime(&t));
等價於:
struct
tm
*ptr;
ptr=localtime(&t);
printf(asctime(ptr));
那麼,下面這個程序的兩條printf語句輸出的結果就是不同的了(除非你將本地時區設為世界標准時間所在的時區):
#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
Ⅸ 13.5怎樣在C語言中取得當前日期或時間
C語言gmtime()函數:獲取當前時間和日期
頭文件:
#include<time.h>
定義函數:
structtm*gmtime(consttime_t*timep);
函數說明:gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm 返回。
結構tm 的定義為
structtm{
inttm_sec;//代表目前秒數,正常范圍為0-59,但允許至61秒
inttm_min;//代表目前分數,范圍0-59
inttm_hour;//從午夜算起的時數,范圍為0-23
inttm_mday;//目前月份的日數,范圍01-31
inttm_mon;//代表目前月份,從一月算起,范圍從0-11
inttm_year;//從1900年算起至今的年數
inttm_wday;//一星期的日數,從星期一算起,范圍為0-6
inttm_yday;//從今年1月1日算起至今的天數,范圍為0-365
inttm_isdst;//日光節約時間的旗標
};
此函數返回的時間日期未經時區轉換,而是UTC 時間。
返回值:返回結構tm 代表目前UTC 時間。
範例
#include<time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d ",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
}
執行結果:
2000/10/28Sat8:15:38