方法一,#includetime.h
網站建設哪家好,找創新互聯公司!專注于網頁設計、網站建設、微信開發、微信小程序開發、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了沿河免費建站歡迎大家使用!
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf("%d\n",p-tm_sec); /*獲取當前秒*/
printf("%d\n",p-tm_min); /*獲取當前分*/
printf("%d\n",8+p-tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p-tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d\n",1+p-tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p-tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d\n",p-tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/
}
方法二.#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;}
擴展資料
1、CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數
_timeb定義在SYS\TIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數
#include stdio.h
#include time.h
int main()
{
time_t timep;
struct tm *p;
time(timep);
p = localtime(timep); //此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
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
);
//獲取當前時間,存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
的方法顯示)
先申明下,這個是我轉百度知道的,經常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語言獲取當天日期函數 c語言獲取當前日期時間
URL標題:http://m.kartarina.com/article18/dogepdp.html
成都網站建設公司_創新互聯,為您提供網站導航、響應式網站、品牌網站建設、網站策劃、網站制作、小程序開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯