本文目錄一覽:
在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
c語言如何輸出當前的日期和時間?
#include stdio.h
#include time.h
int main()
{
time_t t; //time_t是一種類型,定義time_t類型的t
time(t); //取得當前時間
printf(“%s\n”,ctime(t));// ctime(t)將日期轉為字元串並列印
return 0;
}
在c語言中如何獲取當前日期?
#include
要添加這個頭文件。
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計算日期
下面是我做的,你可以參照一下,year1表示開始的年份,year2表示你們現在的年份,其他的看不明白的話再留言
#include “stdio.h”
void main()
{
int days(int,int,int);
int year1,month1,day1,year2,month2,day2,a,b,c;
printf(“\nplease input the begin data:year,month,day\n”);
scanf(“%d,%d,%d”,year1,month1,day1);
if(year1%400==0||(year1%4==0year1%100!=0))
a=366-days(year1,month1,day1);
else
a=365-days(year1,month1,day1);
printf(“there are %d days left on %d.\n”,a,year1);
printf(“please input the end data:year,month,day\n”);
scanf(“%d,%d,%d”,year2,month2,day2);
b=days(year2,month2,day2);
c=zhong(year1,year2);
if(year2-year11)
printf(“total are %d days\n”,a+b+c);
else
printf(“total are %d days\n”,a+b);
getch();
}
int days(int x,int y,int z)
{
int sum,leap;
switch(y)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf(“\ndata error”);break;
}
sum=sum+z;
if(x%400==0||(x%4==0x%100!=0))
leap=1;
else
leap=0;
if(leap==1y2)
sum++;
getch();
return sum;
}
int zhong(int year1,int year2)
{
int i,sum;
sum=0;
printf(“please input two year data:year1,year2\n”);
scanf(“%d,%d”,year1,year2);
for(i=year1+1;iyear2;i++)
{
if(i%400==0||(i%4==0i%100!=0))
sum=sum+366;
else
sum=sum+365;
}
return sum;
}
C語言功能:獲取當前日期時間
#include stdio.h
#include time.h
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;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/269873.html