【问题标题】:Convert string datetime in C++在 C++ 中转换字符串日期时间
【发布时间】:2013-02-21 06:34:39
【问题描述】:

我有一个日期表示为“2012-10-28”格式的字符串,我想将其转换为“28/10/2012”的字符串格式。这在 C++ MS Visual Studio 中是否可以使用预定义的函数?

【问题讨论】:

  • 您是在谈论将字符串中的所有“-”替换为“/”,还是需要一些时间函数来做到这一点?

标签: c++ string date time


【解决方案1】:

strptime 不幸的是在 Windows 中不存在。在这里寻求帮助:strptime() equivalent on Windows?

然后您可以使用strftime 写入日期。

【讨论】:

    【解决方案2】:

    请看COleDateTime::ParseDateTime

    如果不想使用 COleDateTime,则 ParseDateTime 的实现只是 VarDateFromStr 的一个薄包装。

    【讨论】:

      【解决方案3】:

      我是这样解决的:

       Use sscan_f to break date into year, month and day.
       Create struct tm with the data above.
       Use strftime to convert from tm to string with the desired format.
      

      【讨论】:

        【解决方案4】:

        这样就可以了:

        #include <cstdio>
        #include <iostream>
        #include <string>
        using namespace std;
        
        string format_date(string s)
        {
            char buf[11];
            int a, b, c;
            sscanf(s.c_str(), "%d-%d-%d", &a, &b, &c);
            sprintf(buf, "%02d/%02d/%d", c, b, a);
            return buf;
        }
        
        int main()
        {
            cout << format_date("2012-09-28") << endl;
        }
        

        【讨论】:

          【解决方案5】:

          在 Qt 中(一些嵌入式系统还不支持新的计时器类,所以在这里) 我在这里只是给出了如何在没有太多笨拙的情况下转换字符串的想法。 无论如何,计时器类都有纪元功能。


          QString fromSecsSinceEpoch(qint64 epoch)
          {
              QTextStream ts;
              time_t result = epoch;//std::time(NULL);
              //std::cout << std::asctime(std::localtime(&result))
              //            << result << " seconds since the Epoch\n";
              ts << asctime(gmtime(&result));
              return ts.readAll();
          }
          qint64 toSecsSinceEpoch(QString sDate)//Mon Nov 25 00:45:23 2013
          {
              QHash <QString,int> monthNames;
              monthNames.insert("Jan",0);
              monthNames.insert("Feb",1);
              monthNames.insert("Mar",2);
              monthNames.insert("Apr",3);
              monthNames.insert("May",4);
              monthNames.insert("Jun",5);
              monthNames.insert("Jul",6);
              monthNames.insert("Aug",7);
              monthNames.insert("Sep",8);
              monthNames.insert("Oct",9);
              monthNames.insert("Nov",10);
              monthNames.insert("Dec",11);
          
          
              QStringList l_date = sDate.split(" ");
              if (l_date.count() != 5)
              {
                  return 0;//has to be 5 cuz Mon Nov 25 00:45:23 2013
              }
              QStringList l_time = l_date[3].split(":");
              if (l_time.count() != 3)
              {
                  return 0;//has to be 3 cuz 00:45:23
              }
          
              struct tm result;
              result.tm_mday=l_date[2].toInt();
              result.tm_mon=monthNames[l_date[1]];
              result.tm_year=l_date[4].toInt()-1900;;
          
              result.tm_hour=l_time[0].toInt();
              result.tm_min=l_time[1].toInt();
              result.tm_sec=l_time[2].toInt();
              time_t timeEpoch=mktime(&result);
              qDebug()<<"epochhhh :"<<timeEpoch;
              return timeEpoch;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-07
            • 1970-01-01
            • 1970-01-01
            • 2010-12-08
            • 2013-12-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多