2013-07-10 202 views
0

我有一個string date,我需要將它格式化爲日期(dd.mm.yyyy)。我該怎麼做?是否有一些可以簡化格式化的功能? 「編寫一個幫助你管理任務的C++程序...任務具有唯一的格式,id,描述,日期(格式爲」dd.MM.yyyy「的字符串,例如10.07.2013) 「。格式化字符串作爲日期

+0

什麼是* 「'串date'」 *系統時間?像「2013年7月10日」? – Zeta

+0

一個名爲date的字符串。 – Matt

+0

它包含什麼? – jrok

回答

0

你必須更具體的你正在使用的C++。如果它的C++/CLI,那麼你可以使用

DateTime::Parse 

如果它不是C++/CLI,你知道該字符串的準確格式,你可以使用

sscanf(....) 

,並提取個別項目分配時間結構。

+5

如果它是C++/CLI,它應該被標記爲這樣並且沒有C++標記。 「 – chris

0

C++標準庫不提供時間數據類型,但可以通過在頭文件中包含ctime來完成。

#include <ctime> 
#include <iostream> 
using namespace std; 

int main() { 
    time_t t = time(0); // get time now 
    struct tm * now = localtime(& t); 
    cout << (now->tm_year + 1900) << '-' 
     << (now->tm_mon + 1) << '-' 
     << now->tm_mday 
     << endl; 
} 
+4

」類模板std :: chrono :: time_point表示一個時間點。「我會說這是合格的。 – chris

1

試試這個,這給你一個char [32]

time_t curtime; 
struct tm *loctime; 
char date_str[32]; 

curtime = time (NULL); 

/* Convert it to local time representation. */ 
loctime = localtime (&curtime); 
strftime (date_str, 32, "%d.%m.%Y", loctime);