當前位置:首頁 » 編程語言 » c語言取當前時間

c語言取當前時間

發布時間: 2023-02-28 06:49:41

① 問在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.這種方法比較高級

#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<stdio.h>
#include<time.h>
intmain()
{
time_tt;//time_t是一種類型,定義time_t類型的t
time(&t);//取得當前時間
printf("%s ",ctime(&t));//ctime(&t)將日期轉為字元串並列印
return0;
}

熱點內容
開伺服器時如何開pvp 發布:2025-09-17 03:02:23 瀏覽:966
安卓手機序列號a開頭什麼意思 發布:2025-09-17 02:59:26 瀏覽:686
機房電腦重裝以後伺服器不能識別 發布:2025-09-17 02:59:17 瀏覽:154
百度雲伺服器主機地址 發布:2025-09-17 02:59:17 瀏覽:52
dijkstra演算法過程圖解 發布:2025-09-17 02:50:46 瀏覽:310
怎麼看自己我的世界伺服器ip 發布:2025-09-17 02:39:43 瀏覽:896
abapsqlin 發布:2025-09-17 02:37:18 瀏覽:989
安卓手機怎麼恢復ps的照片 發布:2025-09-17 02:14:22 瀏覽:419
android仿百度 發布:2025-09-17 02:12:53 瀏覽:604
木瓜奇跡腳本 發布:2025-09-17 01:54:25 瀏覽:410