Results 1 to 4 of 4

Thread: Converting a QString to QDate

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Converting a QString to QDate

    Hi!
    I am struggling right now with the conversion of a string to a date, but I am not succeeding. Could someone help?

    My string is in the following format:
    Sa Feb 22 2014
    .

    After compiling, qDebug prints an empty QDate (
    QDate("")
    )

    I have the following code:

    Qt Code:
    1. QString strDate1, strDate2;
    2. QDate strDate;
    3.  
    4. strDate1 = Sa Feb 22 2014;
    5. strDate2 = QString::fromStdString(strDate1.toStdString());
    6. strDate.fromString(strDate2, "dd mmm ww yyyy");
    To copy to clipboard, switch view to plain text mode 

    Many thanks in advance
    Last edited by Stanfillirenfro; 31st March 2014 at 12:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Converting a QString to QDate

    Line 4 of your code is missing quotes.

    Line 5 is a needless two way conversion, strDate1 is already a QString

    Line 6 calls a static method (fromString) like a member method. The call's return value is not used.
    "dd" means a day number, "mmm" does not exist in the date context, "ww" does not exist in date formats.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Converting a QString to QDate

    This is a typical way to do it.
    Qt Code:
    1. QString str = "Sat Feb 22 2014";
    2. QDate strDate;
    3.  
    4. strDate = QDate::fromString(str, "ddd MMM dd yyyy");
    5.  
    6. qDebug() << strDate;
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting a QString to QDate

    Hi Reddy!
    You are just an Angel!!!! Your code has solved the problem. It is exactly what I wanted to have.

    Many thanks for your help.

Similar Threads

  1. Converting short to QString
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2014, 06:46
  2. Qstring to Qdate and vice versa
    By sai_3289 in forum Qt Programming
    Replies: 10
    Last Post: 12th February 2013, 11:54
  3. Converting a QString to a LPCTSTR?
    By dobedidoo in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 14:27
  4. Converting VC++ qreal to QDate
    By Krish_ng in forum Qt Programming
    Replies: 13
    Last Post: 12th January 2008, 19:23
  5. QString/QDate in KDevelop-Debugger (Qt4)
    By niko in forum KDE Forum
    Replies: 1
    Last Post: 23rd June 2006, 23:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.