方法一,#includetime.h

成都創(chuàng)新互聯(lián)公司自2013年起,先為鐘山等服務(wù)建站,鐘山等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為鐘山企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
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;}
擴展資料
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ù)
C語言中讀取系統(tǒng)時間的函數(shù)為time(),其函數(shù)原型為:
#include time.h
time_t time( time_t * ) ;
time_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現(xiàn)在的的秒數(shù)。可以調(diào)用ctime()函數(shù)進行時間轉(zhuǎn)換輸出:
char * ctime(const time_t *timer);
將日歷時間轉(zhuǎn)換成本地時間,按年月日格式,進行輸出,如:
Wed Sep 23 08:43:03 2015
C語言還提供了將秒數(shù)轉(zhuǎn)換成相應(yīng)的時間結(jié)構(gòu)的函數(shù):
struct tm * gmtime(const time_t *timer); //將日歷時間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間(即格林尼治時間)
struct tm * localtime(const time_t * timer); //將日歷時間轉(zhuǎn)化為本地時間
將通過time()函數(shù)返回的值,轉(zhuǎn)換成時間結(jié)構(gòu)struct tm :
struct tm {
int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */
int tm_min; /* 分 - 取值區(qū)間為[0,59] */
int tm_hour; /* 時 - 取值區(qū)間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區(qū)間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */
int tm_year; /* 年份,其值等于實際年份減去1900 */
int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數(shù) – 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標(biāo)識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};
編程者可以根據(jù)程序功能的情況,靈活的進行日期的讀取與輸出了。
例如:
#includetime.h
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 "time.h"
time() 取得本地時間(日期時間函數(shù))
settimeofday() 設(shè)置當(dāng)前時間戳
mktime() 將時間結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成經(jīng)過的秒數(shù)
localtime() 獲取當(dāng)?shù)啬壳皶r間和日期
gmtime() 獲取當(dāng)前時間和日期
gettimeofday() 獲取當(dāng)前時間
ctime() 將時間和日期以字符串格式表示
asctime() 將時間日期以字符串格式表示
網(wǎng)站欄目:c語言輸出時間的函數(shù),c語言時間函數(shù)怎么輸入
本文來源:http://chinadenli.net/article30/dsgcopo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信小程序、定制開發(fā)、品牌網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)