方法一,#includetime.h
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了沿河免費建站歡迎大家使用!
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf("%d\n",p-tm_sec); /*獲取當(dāng)前秒*/
printf("%d\n",p-tm_min); /*獲取當(dāng)前分*/
printf("%d\n",8+p-tm_hour);/*獲取當(dāng)前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p-tm_mday);/*獲取當(dāng)前月份日數(shù),范圍是1-31*/
printf("%d\n",1+p-tm_mon);/*獲取當(dāng)前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p-tm_year);/*獲取當(dāng)前年份,從1900開始,所以要加1900*/
printf("%d\n",p-tm_yday); /*從今年1月1日算起至今的天數(shù),范圍為0-365*/
}
方法二.#include?stdio.h
#include?time.h
int?main?()
{
time_t?t
struct?tm?*?lt;????time?(t);//獲取Unix時間戳。
lt?=?localtime?(t);//轉(zhuǎn)為時間結(jié)構(gòu)。
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);//輸出結(jié)果
return?0;}
擴(kuò)展資料
1、CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當(dāng)前系統(tǒng)時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數(shù)
_timeb定義在SYS\TIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數(shù)
#include stdio.h
#include time.h
int main()
{
time_t timep;
struct tm *p;
time(timep);
p = localtime(timep); //此函數(shù)獲得的tm結(jié)構(gòu)體的時間,是已經(jīng)進(jìn)行過時區(qū)轉(zhuǎn)化為本地時間
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;
}
#include
要添加這個頭文件。
time_t
rawtime;
struct
tm
*
target_time;
time
(
rawtime
);
//獲取當(dāng)前時間,存rawtime里
target_time
=
localtime
(
rawtime
);
//獲取當(dāng)?shù)貢r間
利用struct
tm,你可以按需取出年月日時分秒星期幾等數(shù)值。
---------------------
你的問題:
time_t
now;
long
int
dt=3600;
//
時間長度,秒數(shù)
now
=
time
(NULL);
//獲取當(dāng)前時間
printf("%s
",ctime(now));
//直接打印時間
now=now+dt;
printf("%s
",ctime(now));
//
直接打印加dt后的時間
(當(dāng)然,你也可以用
ctime(now)
返回的字符串
通過
MFC
的方法顯示)
先申明下,這個是我轉(zhuǎn)百度知道的,經(jī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 -- 必須的時間函數(shù)頭文件
time_t -- 時間類型(time.h 定義)
struct tm -- 時間結(jié)構(gòu),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 ); -- 轉(zhuǎn)為當(dāng)?shù)貢r間,tm 時間結(jié)構(gòu)
asctime ()-- 轉(zhuǎn)為標(biāo)準(zhǔn)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
其它你一目了然啦。