1樓:亞信小狼
time_t ttime; //距2023年1月1日的秒數char str[80];
struct tm* sttm; //時間結構,格式請見其它回答time(&timer);
sttm = localtime(&ttime);
strftime(str,sizeof(str),"%y%m%d",sttm);//格式化時間
printf(str);//輸出
另外,格式化時間的格式說明如下,希望對你有幫助:
%a 星期幾的簡寫
%a 星期幾的全稱
%b 月分的簡寫
%b 月份的全稱
%c 標準的日期的時間串
%c 年份的後兩位數字
%d 十進位制表示的每月的第幾天
%d 月/天/年
%e 在兩字元域中,十進位制表示的每月的第幾天%f 年-月-日
%g 年份的後兩位數字,使用基於周的年
%g 年分,使用基於周的年
%h 簡寫的月份名
%h 24小時制的小時
%i 12小時制的小時
%j 十進位制表示的每年的第幾天
%m 十進位制表示的月份
%m 十時製表示的分鐘數
%n 新行符
%p 本地的am或pm的等價顯示
%r 12小時的時間
%r 顯示小時和分鐘:hh:mm
%s 十進位制的秒數
%t 水平製表符
%t 顯示時分秒:hh:mm:ss
%u 每週的第幾天,星期一為第一天 (值從0到6,星期一為0)%u 第年的第幾周,把星期日做為第一天(值從0到53)%v 每年的第幾周,使用基於周的年
%w 十進位制表示的星期幾(值從0到6,星期天為0)%w 每年的第幾周,把星期一做為第一天(值從0到53)%x 標準的日期串
%x 標準的時間串
%y 不帶世紀的十進位制年份(值從0到99)%y 帶世紀部分的十制年份
%z,%z 時區名稱,如果不能得到時區名稱則返回空字元。
%% 百分號
2樓:犍為真人
#include "time.h"
time_t time(time_t *timer);
呼叫後將當前系統時間與2023年1月1日相差的秒數存入到timer中,timer可看成是一個長整型數
struct tm* localtime(const time_t *timer)
將time()函式呼叫的結果做為引數傳入到localtime()函式中就能得到當前時間和日期,注意得到的年是和1970的差值,月份是和1月的差值
struct tm是一個結構體,定義如下
struct tm
求當前時間的示例
int getsystemtime()
其他時間的函式和結構還有:
timeval結構
#include
struct timeval
; tms結構
儲存著一個程序及其子程序使用的cpu時間
struct tms
timer_struct結構
#include
struct timer_struct
utime函式
更改檔案的存取和修改時間
int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1
times 為空指標,存取和修改時間設定為當前時間
struct utimbuf
3樓:青女長孫運傑
一般使用time函式,windows下可以使用gettickcount或timegettime函式獲取系統時間
如何用c語言獲取當前系統時間?
c語言中如何獲取當前系統時間的小時?
c語言中 如何獲取系統時間
4樓:阿里
#include
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*/
擴充套件連結
注意事項:
struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt->tm_year+1900。
5樓:兔丞飛
#include
#include
int main ()
time_t 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;}
擴充套件資料
#include -- 必須的時間函式標頭檔案
time_t -- 時間型別(time.h 定義是typedef long time_t; 追根溯源,time_t是long)
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 ); -- 獲取時間,以秒計,從2023年1月一日起算,存於rawtime
localtime ( &rawtime ); -- 轉為當地時間,tm 時間結構
asctime ()-- 轉為標準ascii時間格式:
星期 月 日 時:分:秒 年
6樓:跪著作揖
獲取系統的時間需要藉助time()函式,具體的**如下:
#include
#include
struct mydate
unsigned year;
unsigned month;
unsigned day;
struct mydate today( )
struct mydate today;
time_t rawtime;
struct tm *timeinfo;
time ( &rawtime );
today.year = timeinfo->tm_year + 1900;
today.month = timeinfo->tm_mon + 1;
today.day = timeinfo->tm_mday;
return today;
int main( )
struct mydate today = today( )
printf("%4d/%02d/%02d\n",today.year,today.month,today.day);
return 0;
擴充套件資料:
#include
#include
int main ( )
time_t t;
struct tm * lt;
獲取unix時間戳。
轉為時間結構。
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;
7樓:匿名使用者
1.首先是在dev c++中新建c語言的程式,如下圖所示2.接下來在c語言檔案中新增main函式,如下圖所示3.
然後在頭部的庫檔案中匯入time.h庫檔案,如下圖所示4.接下來通過庫檔案中的gmtime獲取當前時間,如下圖所示5.
然後我們就可以通過如下圖所示的方式分別獲取時間中的年月日等資訊6.最後我們執行c程式,就可以得到系統時間並獲得時間中的年月日7.綜上所述,在c語言中獲得系統時間一定要先引入time.
h庫,大家快去試一下吧
8樓:
#include
#include
int main( )
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;
tm_year 從2023年計算,所以要加1900,月tm_mon,從0計算,所以要加1
time( &nowtime ); 獲取時間localtime ( &nowtime ); 轉為當地時間
c語言中如何獲取當前系統時間的小時
9樓:倒黴熊
#include
#include
void main ()
***************==
#include -- 必須的時間函式標頭檔案
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 ); -- 獲取時間,以秒計,從2023年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 從2023年計算,所以要加1900,
月tm_mon,從0計算,所以要加1
其它你一目瞭然啦。
如何用c語言輸入123輸出,如何用C語言輸入123,輸出
n 100因為n是整形,所以輸出是百位的數字n 10是取餘,正好是個位的數字 b是中間的數,道理和a是一樣的。再把三個數反過來輸出一遍就行了,本題中是重新組成一個三位數。是這樣的。int n,a,b,c 定義了4個整型變數,其中n為即將輸入的數,a為所輸入數字的百位數,b 為所輸 入數字的十位數,c...
如何自學c語言,如何自學C語言
1 程語語言其實是一個很初級的工具,但是又必須熟練的掌握它,學懂一門程式語言就好像學會了寫字,但是會寫字的人不見得會寫文章,而會寫文章又不見的寫得好。可是如果不會寫字,那就一定寫不出文章來。2 在學習c語言之前,應該學好計算機基礎。裡面的很多概念對於c程式設計師都是非常重要的。如果在著手學習c之前,...
怎麼學習C語言?如何學習c語言?
首先你得有心!必須是真想學,不然三天打魚,兩天曬網是沒有用的。第一,買一本教材,我學的是 c語言程式設計 感覺還不錯,還有一本實驗教材。有了書之後,就要看了,開始是有點難度的,每天看一點背一點。瞭解一點。當然要有電腦,不然沒法實踐是學不好的。開始就是看書。一點一點的瞭解,熟悉,後面的就好學多了,做些...