新聞中心
在c語言中如何使用系統(tǒng)函數(shù)得到當(dāng)前的日期?
獲得日期和時間
創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),謝通門網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:謝通門等地區(qū)。謝通門做網(wǎng)站價格咨詢:13518219792
這里說的日期和時間就是我們平時所說的年、月、日、時、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個名為tm的結(jié)構(gòu)體中,那么如何將一個日歷時間保存為一個tm結(jié)構(gòu)的對象呢?
其中可以使用的函數(shù)是gmtime()和localtime(),這兩個函數(shù)的原型為:
struct
tm
*
gmtime(const
time_t
*timer);
struct
tm
*
localtime(const
time_t
*
timer);
其中g(shù)mtime()函數(shù)是將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),并返回一個tm結(jié)構(gòu)體來保存這個時間,而localtime()函數(shù)
是將日歷時間轉(zhuǎn)化為本地時間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時間是2005年7月30日7點(diǎn)18分20秒,那么我用
localtime()函數(shù)在中國地區(qū)獲得的本地時間會比世界標(biāo)準(zhǔn)時間晚8個小時,即2005年7月30日15點(diǎn)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;
}
運(yùn)行結(jié)果是:
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}是一個空字符,表示字符串結(jié)束。下面是兩個函數(shù)的原型:
Char
*
asctime(const
struct
tm
*
timeptr);
char
*
ctime(const
time_t
*timer);
其中asctime()函數(shù)是通過tm結(jié)構(gòu)來生成具有固定格式的保存時間信息的字符串,而ctime()是通過日歷時間來生成時間字符串。這樣的
話,asctime()函數(shù)只是把tm結(jié)構(gòu)對象中的各個域填到時間字符串的相應(yīng)位置就行了,而ctime()函數(shù)需要先參照本地的時間設(shè)置,把日歷時間轉(zhuǎn)
化為本地時間,然后再生成格式化后的字符串。在下面,如果t是一個非空的time_t變量的話,那么:
printf(ctime(t));
等價于:
struct
tm
*ptr;
ptr=localtime(t);
printf(asctime(ptr));
那么,下面這個程序的兩條printf語句輸出的結(jié)果就是不同的了(除非你將本地時區(qū)設(shè)為世界標(biāo)準(zhǔn)時間所在的時區(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;
}
運(yùn)行結(jié)果:
Sat
Jul
30
08:43:03
2005
Sat
Jul
30
16:43:03
2005
C語言日期函數(shù)設(shè)計(jì)
#includestdio.h
#includestdlib.h
#includeconio.h
#includetime.h
int days(int year,int month,int day)
{
if(month==1||month==2)//判斷month是否為1或2
{
year--;
month+=12;
}
int c=year/100;
int y=year-c*100;
int week=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;
while(week0)week+=7;
week%=7;
return week;
}
int years(int year)
{
if((year%4==0year%100!=0)||(year%400==0))return 1;
else return 0;
}
int months(int year,int month)
{
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
case 2:if(years(year))return 29;
else return 28;
default:return -1;
}
}
void print(int year,int month)
{
system("cls");
if(year0)printf("公元前");
struct tm *p; //時間結(jié)構(gòu)體
time_t t; //把當(dāng)前時間給t
t=time(NULL); //NULL為0
p=localtime(t);//獲取當(dāng)前系統(tǒng)時間
printf("%d年%d月\n(現(xiàn)在是北京時間%d年%d月%d日周",abs(year),month, 1900+p-tm_year, 1+p-tm_mon, p-tm_mday, p-tm_hour, p-tm_min, p-tm_sec);
if(p-tm_wday==1)printf("一");
else if(p-tm_wday==2)printf("二");
else if(p-tm_wday==3)printf("三");
else if(p-tm_wday==4)printf("四");
else if(p-tm_wday==5)printf("五");
else if(p-tm_wday==6)printf("六");
else if(p-tm_wday==7)printf("日");
printf("%d點(diǎn)%d分%d秒)\n", p-tm_hour, p-tm_min, p-tm_sec);
printf("周日 周一 周二 周三 周四 周五 周六\n");
int i,add=1,place=days(year,month,1);
for(i=1;i=place*5;i++)printf(" ");
int place2=7-place;
for(i=1;i=place2;i++)
{printf("%4d ",add);
add++;
}
printf("\n");
while(add=months(year,month))
{
printf("%4d",add);
add++;
if((add-place2)%7==1)printf("\n");
else printf(" ");
}
printf("\n—\t--:上月 --:下月 \t—\n— Esc:退出 空格:設(shè)置年份 —\n");
}
main()
{
struct tm *local; //時間結(jié)構(gòu)體
time_t t; //把當(dāng)前時間給t
t=time(NULL); //NULL為0
local=localtime(t);//獲取當(dāng)前系統(tǒng)時間
int year=1900+local-tm_year,month=1+local-tm_mon;
char input=0;
print(year,month);
while(1)
{
input=getch();
if(input==27)
{system("cls");
printf("\n\n\t謝謝使用\n 請按任意鍵繼續(xù)...\n");
getch();
exit(0);
}
else if(input==75)
{month--;
if(month=0)
{month=12;if(year(-999))year--;if(year==0)year--;}
}
else if(input==77)
{
month++;
if(month=13)
{month=1;year++;if(year==0)year++;}
}
else if(input==' ')
{
while(1)
{ system("cls");
printf("┌────┐\n");
printf("│SetYear │\n");
printf("│%4d 年 │\n",year);
printf("-,-:switch\n");
printf("Enter:choose\n");
printf("└────┘\n");
switch(getch())
{
case 27:system("cls");
printf("\n\n\t謝謝使用\n 請按任意鍵繼續(xù)...\n");
getch();
exit(0);
case 75:if (year(-999))year--;if(year==0)year--;break;
case 77:year++;if(year==0)year++;break;
case 13:goto end;
}
}
}end:
print(year,month);
}
}
我給你一個我剛編的程序(日歷),供您參考
c語言 時間函數(shù)
c語言時間函數(shù):
1、獲得日歷時間函數(shù):
可以通過time()函數(shù)來獲得日歷時間(Calendar Time),其原型為:time_t time(time_t * timer);
如果已經(jīng)聲明了參數(shù)timer,可以從參數(shù)timer返回現(xiàn)在的日歷時間,同時也可以通過返回值返回現(xiàn)在的日歷時間,即從一個時間點(diǎn)(例如:1970年1月1日0時0分0秒)到現(xiàn)在此時的秒數(shù)。如果參數(shù)為空(NUL),函數(shù)將只通過返回值返回現(xiàn)在的日歷時間,比如下面這個例子用來顯示當(dāng)前的日歷時間:
2、獲得日期和時間函數(shù):
這里說的日期和時間就是平時所說的年、月、日、時、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個名為tm的結(jié)構(gòu)體中,那么如何將一個日歷時間保存為一個tm結(jié)構(gòu)的對象呢?
其中可以使用的函數(shù)是gmtime()和localtime(),這兩個函數(shù)的原型為:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
其中g(shù)mtime()函數(shù)是將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),并返回一個tm結(jié)構(gòu)體來保存這個時間,而localtime()函數(shù)是將日歷時間轉(zhuǎn)化為本地時間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時間是2005年7月30日7點(diǎn)18分20秒,那么用localtime()函數(shù)在中國地區(qū)獲得的本地時間會比世界標(biāo)準(zhǔn)時間晚8個小時,即2005年7月30日15點(diǎn)18分20秒。
用c語言如何獲取系統(tǒng)當(dāng)前時間的函數(shù)?
1、C語言中讀取系統(tǒng)時間的函數(shù)為time(),其函數(shù)原型為:\x0d\x0a#include \x0d\x0atime_t time( time_t * ) ;\x0d\x0atime_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現(xiàn)在的的秒數(shù)。\x0d\x0a2、C語言還提供了將秒數(shù)轉(zhuǎn)換成相應(yīng)的時間格式的函數(shù):\x0d\x0a char * ctime(const time_t *timer); //將日歷時間轉(zhuǎn)換成本地時間,返回轉(zhuǎn)換后的字符串指針 可定義字符串或是字符指針來接收返回值\x0d\x0a struct tm * gmtime(const time_t *timer); //將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間),返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a struct tm * localtime(const time_t * timer); //將日歷時間轉(zhuǎn)化為本地時間,返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a3、例程:\x0d\x0a#include \x0d\x0avoid main()\x0d\x0a{\x0d\x0a time_t t;\x0d\x0a struct tm *pt ;\x0d\x0a char *pc ;\x0d\x0a time(t);\x0d\x0a pc=ctime(t) ; printf("ctime:%s", pc );\x0d\x0a pt=localtime(t) ; printf("year=%d", pt-tm_year+1900 );\x0d\x0a}\x0d\x0a\x0d\x0a時間結(jié)構(gòu)體struct tm 說明:\x0d\x0a\x0d\x0astruct tm { \x0d\x0a int tm_sec; /* 秒 _ 取值區(qū)間為[0,59] */ \x0d\x0a int tm_min; /* 分 - 取值區(qū)間為[0,59] */ \x0d\x0a int tm_hour; /* 時 - 取值區(qū)間為[0,23] */ \x0d\x0a int tm_mday; /* 一個月中的日期 - 取值區(qū)間為[1,31] */ \x0d\x0a int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */ \x0d\x0a int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */ \x0d\x0a int tm_wday; /* 星期 _ 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */ \x0d\x0a int tm_yday; /* 從每年的1月1日開始的天數(shù) _ 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */ \x0d\x0a int tm_isdst; /* 夏令時標(biāo)識符,實(shí)行夏令時的時候,tm_isdst為正。不實(shí)行夏令時的進(jìn)候,tm_isdst為0;不了解情況時,tm_isdst()為負(fù)。*/ \x0d\x0a};
C語言的時間函數(shù)
C語言的建時間函數(shù)是 mktime(),原型在 time.h 里
調(diào)用有點(diǎn)繁。
下面,用我的程序輸入 年月日時分秒,調(diào)用mktime(), 就得 C語言 可直接使用的 時間, 存放在 t 里。
例如 輸入年月日時分秒: 2008 8 16 9 55 25
time_t t; 里 就有了 各種時間信息,例如星期幾...
#include stdio.h
#include time.h
void main(){
struct tm *target_time;
time_t rawtime, t;
int year,month,mday,hh,mm,ss;
time ( rawtime );
target_time = localtime ( rawtime );
printf("Please enter year month day hour minute second\n");
printf("For example: \n");
printf("2008 8 16 9 55 25\n");
scanf("%d %d %d %d %d %d", year, month, mday, hh,mm,ss);
target_time-tm_year = year - 1900;
target_time-tm_mon= month - 1;
target_time-tm_mday = mday ;
target_time-tm_hour = hh ;
target_time-tm_min = mm ;
target_time-tm_sec = ss ;
//
t = mktime (target_time);
// t is ready to use
printf("%s ",ctime(t));
}
C語言獲取系統(tǒng)時間
需要利用C語言的時間函數(shù)time和localtime,具體說明如下:
一、函數(shù)接口介紹:
1、time函數(shù)。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結(jié)構(gòu)體,一般為長整型。
這個函數(shù)會獲取當(dāng)前時間,并返回。 如果參數(shù)__timer非空,會存儲相同值到__timer指向的內(nèi)存中。
time函數(shù)返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經(jīng)過的秒數(shù),不考慮閏秒。
由于是秒作為單位的,所以這并不是習(xí)慣上的時間,要轉(zhuǎn)為習(xí)慣上的年月日時間形式就需要另外一個函數(shù)了。
2、localtime函數(shù)。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結(jié)構(gòu)體,包含了年月日時分秒等信息。
這種結(jié)構(gòu)是適合用來輸出的。
分享標(biāo)題:c語言系統(tǒng)日期函數(shù) c語言 日期
鏈接URL:http://fisionsoft.com.cn/article/hpjegi.html