user1845360
user1845360

Reputation: 857

Convert string datetime in C++

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 ?

Upvotes: 0

Views: 5335

Answers (5)

Biddut Mitra
Biddut Mitra

Reputation: 1

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

Upvotes: 0

artahian
artahian

Reputation: 2093

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

Upvotes: 3

user1845360
user1845360

Reputation: 857

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.

Upvotes: 2

Lucian
Lucian

Reputation: 3554

Please look at COleDateTime::ParseDateTime.

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

Upvotes: 1

Dariusz
Dariusz

Reputation: 22251

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

You can then write the date using strftime.

Upvotes: 0

Related Questions