2012-08-15 69 views
0

我想在我的C++程序中顯示當前的日期和時間。我簡單地通過這些代碼行來做到這一點:無法在Xcode中使用_strdate(C++)

char date[9], time[9]; 

_strdate(date); 
_strtime(time); 

cout << "Current date and time: " << date << " " << time << endl; 

這在MS Visual C++ 2010和Dev C++ 4.9.9.2中完美的工作。但是這在Xcode中不起作用!它說我正在使用未聲明的標識符'strdate'和'strtime'。我已經包括iostreamctime庫,但這並沒有幫助。有什麼問題?提前致謝。

+0

有沒有理由這應該工作。 – 2012-08-15 11:26:45

回答

1
std::string _strdate_alternative() 
{ 
char cptime[50]; 
time_t now = time(NULL); 
strftime(cptime, 50, "%b. %d, %Y", localtime(&now)); //short month name 
return std::string(cptime); 
} 

如前所述,標準c/C++庫中沒有這樣的函數。 你可以使用這個選擇。代碼基於互聯網上的一些信息。我沒有編譯它。

+0

非常感謝,它完美地顯示了日期並顯示了我這樣做的時間: time_t rawtime; struct tm * timeinfo; char current_time [80]; time(&rawtime); timeinfo = localtime(&rawtime); strftime(current_time,80,「%H:%M」,timeinfo); – Yashman 2012-08-15 12:55:20

1

這些函數不是標準C++的一部分;它們是特定於Microsoft的擴展,因此可能無處不在。

對於類似的功能,您可以嘗試合併time,localtimestrftime