我知道,與的ctime像這樣獲取日期時間用C
time_t now;
time(&now);
fprintf(ft,"%s",ctime(&now));
使用返回我的日期時間這樣
Tue Jun 18 12:45:52 2013
我的問題是,如果有與的ctime到類似的東西獲取時間這種格式
2013/06/18 10:15:26
我知道,與的ctime像這樣獲取日期時間用C
time_t now;
time(&now);
fprintf(ft,"%s",ctime(&now));
使用返回我的日期時間這樣
Tue Jun 18 12:45:52 2013
我的問題是,如果有與的ctime到類似的東西獲取時間這種格式
2013/06/18 10:15:26
使用strftime
#include <stdio.h>
#include <time.h>
int main()
{
struct tm *tp;
time_t t;
char s[80];
t = time(NULL);
tp = localtime(&t);
strftime(s, 80, "%Y/%m/%d %H:%M:%S", tp);
printf("%s\n", s);
return 0;
}
見localtime和聯機幫助頁。第一個函數使用日期/時間元素轉換結構中的時間戳,第二個函數使用格式字符串將其轉換爲字符串。
破碎下降時間被存儲在其在如下所定義的結構TM:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
它可以顯示在我們希望的格式單個變量實現。
#include <stdio.h>
#include <time.h>
int main(void){
FILE *ft = stdout;
char outbuff[32];
struct tm *timeptr;
time_t now;
time(&now);
timeptr = localtime(&now);
strftime(outbuff, sizeof(outbuff), "%Y/%m/%d %H:%M:%S", timeptr);//%H:24 hour
fprintf(ft,"%s", outbuff);
return 0;
}
這個工作正常。唯一的問題是我在while循環中使用它,並在同一時間在每個循環中返回我。我如何修改它以在每個循環中將新時間還給我?使用memset也許? – dali1985
然後你必須在每個循環中調用time()和localtime() –