About using QDateTime::fromString



  • I wanna translate time string from sqlite to QDateTime , but It seamed failed , here's the code :
    @

    QDateTime date = QDateTime::fromString( "2013-1-12 14:53:42" , "yyyy-MM-dd hh:mm:ss" );
    QString strDate,strTime;
    do
    {
    strDate = date.date().toString();//empty
    strTime = date.time().toString();//empty
    } while (false);
    @

    what's wrong?



  • Try using
    @
    date.toString("yyyy-MM-dd");
    @

    instead of

    @
    date.date().toString();
    @

    and to obtain the time value, use

    @
    date.toString("hh:mm:ss");
    @

    I haven't tried it on my computer yet.



  • @
    strDate = date.toString("yyyy-MM-dd"); //empty
    strTime = date.toString("hh:mm:ss");//empty
    @
    and
    @
    strDate = date.date().toString("yyyy-MM-dd");//empty
    strTime = date.time().toString("hh:mm:ss");//empty
    @



  • From what I understood, you are trying to retrieve a string from the database and convert it to QDateTime format. Then, why you are converting it back to a string to check for it's emptiness?
    Even if you must do that, you can simply use @strDate = date.toString("yyyy-MM-dd"); //empty@

    and @strTime = date.toString("hh:mm:ss");//empty@



  • has fixed , that's should be follow code:
    @
    QDateTime date = QDateTime::fromString( "2013-1-12 14:53:42" , "yyyy-M-d h:m:s" );
    QString strDate,strTime;
    do
    {
    strDate = date.date().toString();//empty
    strTime = date.time().toString();//empty
    } while (false);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.