Results 1 to 1 of 1

Thread: Convert QString to QDateTime with fromString()

  1. #1
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Convert QString to QDateTime with fromString()

    Hi folks,

    this has to be something very simple but I don't know what I do wrong:

    Qt Code:
    1. QString str = "23/02/2024,00:46:52";
    2. QString format = "dd/mm/yyyy,hh:mm:ss";
    3. QDateTime dt = QDateTime::fromString(str, format);
    4. qDebug() << "dt:" << dt << ",dt.isValid():" << dt.isValid();
    To copy to clipboard, switch view to plain text mode 

    Output is:
    dt: QDateTime(Invalid) ,dt.isValid(): false
    I tried it on Qt-versions 4.8.7, 5.15.2 and 6.2.4.
    All do the same! Why is that??? I also changed the str and the format a bit but still in will not do the desired...
    Any hints?

    Thx in advance
    freeman_w


    Added after 6 minutes:


    OMG, the month formatting was wrong...

    It should be "dd/MM/yyyy,hh:mm:ss"

    I am sorry folks... I said it HAD to be very simple...
    Last edited by freeman_w; 14th February 2024 at 21:55.

Similar Threads

  1. Replies: 7
    Last Post: 5th September 2016, 13:14
  2. Getting problems to convert from string to QDateTime
    By cesroc in forum Qt Programming
    Replies: 6
    Last Post: 2nd April 2012, 12:37
  3. convert decimal seconds to QDateTime and back
    By mcarter in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 13:06
  4. QDateTime fromString
    By mklieber in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 22:15
  5. How to convert unix time to QDateTime
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2007, 01:25

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.