首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY)

日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY)
EN

Stack Overflow用户
提问于 2013-10-14 09:18:09
回答 1查看 1K关注 0票数 1

好的,我使用了这种串流技术,以便从.txt文件中提取类型、日期、分数等对象,并在稍后的代码中将其推回到向量中。我的问题是,输入文件中的日期是YYYY/MM/DD格式,我需要将其转换为MM/DD/YYYY,但我不熟悉如何做到这一点。有人告诉我使用子字符串方法是可行的,但我是C++的新手,所以有人知道如何解决我的这个问题吗?

代码语言:javascript
复制
void LineData :: Set_Line (const string s)
{
    stringstream temp (s);

    temp >> type;

    temp >> date;

    string max;
    temp >> max;
    max_score = atoi (max.c_str());

    string actual;
    temp >> actual;
    actual_score = atof (actual.c_str());

    ws(temp);
    getline(temp, name);
}
EN

回答 1

Stack Overflow用户

发布于 2013-10-14 10:40:34

这是可以做到的一种方法:

代码语言:javascript
复制
//this will split-up the string
int i = 0;
stringstream ssdate(date); //date needs to be stringstream

while (getline(ssdate, part, '/'))
{
    if(i == 0) year = part;
    if(i == 1) month = part;
    if(i == 2) day = part;
    i++; //counter
}

//now all you have to do is arrage them how you need them
//If you want me to show you that I can
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19352197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档