2013-07-10 204 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);