0

I have a date represented as string in the format "2012-10-28" and I want to convert it in the string format of "28/10/2012". Is this possible in C++ MS Visual Studio using a predefined function ?

user1845360
  • 847
  • 2
  • 12
  • 29
  • 1
    are you talking about replacing all "-" to "/" in your string or you need some Time function to do that? – Abhineet Mar 06 '13 at 13:25

5 Answers5

3

This will do it:

#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;
}
artahian
  • 2,093
  • 14
  • 17
2

I worked it out that way:

 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.
user1845360
  • 847
  • 2
  • 12
  • 29
1

Please look at COleDateTime::ParseDateTime.

If do not want to use COleDateTime the implementation of the ParseDateTime is just a thin wrapper around VarDateFromStr.

Lucian
  • 3,407
  • 2
  • 21
  • 18
0

strptime unfortunately does not exist in windows. Seek help here: strptime() equivalent on Windows?

You can then write the date using strftime.

Community
  • 1
  • 1
Dariusz
  • 21,561
  • 9
  • 74
  • 114
0

in Qt (some embedded system does not support new timer class yet, so here) I here just give the idea how to convert a string without much mumbo jumbo. the timer class has the epoch function anyway.


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;
}