你的代碼沒有錯,在DOS的C下沒有問題。但DOS的有些時間函數在C++編譯器下已經無效了。可嘗試用下面的——

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供臨海網站建設、臨海做網站、臨海網站設計、臨海網站制作等企業(yè)網站建設、網頁設計與制作、臨海企業(yè)網站模板建站服務,十年臨海做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
#include "stdio.h"
#include "time.h"
#include DOS.H
void main(void){
struct tm *pt;
time_t t;
t=time(NULL);
pt=localtime(t);
printf("The current time is: %2d:%02d:%02d\n",
pt-tm_hour, pt-tm_min, pt-tm_sec);
}
但tm結構沒有ms級變量。
#includeconin.h
#include dos.h
或使用
#include time.h
time_t t;
time(t);
puts(ctime(t));
二者均定義在time.h中。
1 在C語言中,為了操作簡單,減少引入頭文件的數量,相關功能的類型及函數均會定義在同一頭文件中,比如輸入輸出相關的均定義在stdio.h中,而時間相關的均定義在time.h中。
2 time結構體,即struct time, 是用來存儲時間的結構體。
3 gettime函數,為獲取時間函數,其參數為struct time *類型。
另外,在不確定是存儲在哪個頭文件,即編程時不確定要引用哪個頭文件時,可以在系統(tǒng)頭文件文件夾中,進行全文搜索,從而得知要需要的頭文件,及對應的使用方式。
先申明下,這個是我轉百度知道的,經常BAIDU一下,就OK了
#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 ( "%4d-%02d-%02d %02d:%02d:%02d\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
其它你一目了然啦。
需要利用C語言的時間函數time和localtime,具體說明如下:
一、函數接口介紹:
1、time函數。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結構體,一般為長整型。
這個函數會獲取當前時間,并返回。 如果參數__timer非空,會存儲相同值到__timer指向的內存中。
time函數返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。
由于是秒作為單位的,所以這并不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函數了。
2、localtime函數。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結構體,包含了年月日時分秒等信息。
這種結構是適合用來輸出的。
二、參考代碼:
#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;
}
注意事項:
struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt-tm_year+1900。
#include "time.h"
time_t time(time_t *timer);
調用后將當前系統(tǒng)時間與1900年1月1日相差的秒數存入到timer中,timer可看成是一個長整型數
struct tm* localtime(const time_t *timer)
將time()函數調用的結果做為參數傳入到localtime()函數中就能得到當前時間和日期,注意得到的年是和1970的差值,月份是和1月的差值
struct tm是一個結構體,定義如下
struct tm
{
int tm_sec; //當前秒
int tm_min; //當前分鐘
int tm_hour; //當前小時
int tm_mday; //當前在本月中的天,如11月1日,則為1
int tm_mon; //當前月,范圍是0~11
int tm_year; //當前年和1900的差值,如2006年則為36
int tm_wday; //當前在本星期中的天,范圍0~6
int tm_yday; //當前在本年中的天,范圍0~365
int tm_isdst; //這個我也不清楚
}
求當前時間的示例
int getSystemTime()
{
time_t timer;
struct tm* t_tm;
time(timer);
t_tm = localtime(timer);
printf("today is %4d%02d%02d%02d%02d%02d\n", t_tm.tm_year+1900,
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);
return 0;
}
其他時間的函數和結構還有:
timeval結構
#include include/linux/time.h
struct timeval
{
time_t tv_sec;
susecond_t tv_usec; //當前妙內的微妙數
};
tms結構
保存著一個進程及其子進程使用的cpu時間
struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
timer_struct結構
#include include/linux/timer.h
struct timer_struct
{
unsigned long expires; //定時器被激活的時刻
void (*fn)(void); //定時器激活后的處理函數
}
utime函數
更改文件的存取和修改時間
int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1
times 為空指針,存取和修改時間設置為當前時間
struct utimbuf
{
time_t actime;
time_t modtime;
}
當前文章:gettime函數c語言,c語言datetime
本文路徑:http://www.kartarina.com/article20/hdpgjo.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網站建設、網站排名、動態(tài)網站、做網站、外貿建站、響應式網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)