十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
#include?stdio.h?

我們提供的服務(wù)有:網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、饒陽(yáng)ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的饒陽(yáng)網(wǎng)站制作公司
#include?time.h?
int?main()
{?
time_t?rawtime;?
struct?tm?*?timeinfo;?
time?(?rawtime?);?
timeinfo?=?localtime?(?rawtime?);?
printf?(?"當(dāng)前系統(tǒng)時(shí)間:?%s",?asctime?(timeinfo)?);?
return?0;
}
說明:
time_t // 時(shí)間類型(time.h 定義)?
struct tm { // 時(shí)間結(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 ); // 獲取時(shí)間,以秒計(jì),從1970年1月一日起算,存于rawtime?
localtime ( rawtime ); //轉(zhuǎn)為當(dāng)?shù)貢r(shí)間,tm 時(shí)間結(jié)構(gòu)?
asctime() // 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時(shí)間格式:?
//就是直接打印tm,tm_year 從1900年計(jì)算,所以要加1900,月tm_mon,從0計(jì)算,所以要加1
方法一,#includetime.h
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)前時(shí),這里獲取西方的時(shí)間,剛好相差八個(gè)小時(shí)*/
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時(shí)間戳。
lt?=?localtime?(t);//轉(zhuǎn)為時(shí)間結(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類
如果想計(jì)算兩段時(shí)間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計(jì)算當(dāng)前系統(tǒng)時(shí)間與時(shí)間t1的間隔
int iDay=span.GetDays(); //獲取這段時(shí)間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時(shí)
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數(shù)
_timeb定義在SYS\TIMEB.H,有四個(gè)fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數(shù)
需要利用C語(yǔ)言的時(shí)間函數(shù)time和localtime,具體說明如下:
一、函數(shù)接口介紹:
1、time函數(shù)。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結(jié)構(gòu)體,一般為長(zhǎng)整型。
這個(gè)函數(shù)會(huì)獲取當(dāng)前時(shí)間,并返回。 如果參數(shù)__timer非空,會(huì)存儲(chǔ)相同值到__timer指向的內(nèi)存中。
time函數(shù)返回的為unix時(shí)間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經(jīng)過的秒數(shù),不考慮閏秒。
由于是秒作為單位的,所以這并不是習(xí)慣上的時(shí)間,要轉(zhuǎn)為習(xí)慣上的年月日時(shí)間形式就需要另外一個(gè)函數(shù)了。
2、localtime函數(shù)。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個(gè)結(jié)構(gòu)體,包含了年月日時(shí)分秒等信息。
這種結(jié)構(gòu)是適合用來輸出的。
1、函數(shù)名:
settime
功
能:
設(shè)置系統(tǒng)時(shí)間
原型:void
settime
2、例程:
#include stdio.h
#include dos.h
int main(void)
{
struct time t;
gettime(t);
printf("The current minute is: %d\n", t.ti_min);
printf("The current hour is: %d\n", t.ti_hour);
printf("The current hundredth of a second is: %d\n", t.ti_hund);
printf("The current second is: %d\n", t.ti_sec);
/* Add one to the minutes struct element and then call settime */
t.ti_min++;
settime(t); //設(shè)置系統(tǒng)時(shí)間
return 0;
}