Results 1 to 11 of 11

Thread: Getting QDate from string

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Getting QDate from string

    Hello,

    I can parse "31 Jan 2009" by the code:

    Qt Code:
    1. QString day=datelist[1]; //datelist contains 31 and Jan and 2009 here...
    2. QString month=datelist[2];
    3. QString year =datelist[3];
    4. QDate todate= QDate::fromString(day+"/"+month+"/"+year,"dd/MMM/yyyy");
    To copy to clipboard, switch view to plain text mode 

    But the same code cannot parse a date with february like "01 Feb 2009" . What is the problem with Feb?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting QDate from string

    try
    Qt Code:
    1. QDate::fromString(input, "dd MMM yyyy");
    To copy to clipboard, switch view to plain text mode 

    If you produce the output with QDate::toString(), just use the same format both times.
    If you use no format for toString(), use none for fromString().

    To your problem:
    where does your input come from?
    In case it is user input: Perhaps you have a loc ale set that happens to have "Jan" for january but something different from "Feb" for february?
    (check by printing qDebug() << date.toString() )

    HTH

  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting QDate from string

    Actually I am parsing an email header like:

    Qt Code:
    1. ....
    2. Date: Sun, 1 Feb 2009 18:51:32 +0200
    3. Message-ID: <c56e0c650902010851u7edd6c83p3a86fc07a9b6957b@mail.gmail.com>
    4. Subject: konux119
    5. From: george wi <yrtgs@gmail.com>
    6. To: Pr Test1 <prtest1@ford.com.tr>
    7. Content-Type: multipart/alternative;
    8. boundary=0016364571aa18aff90461de40d2
    9. X-TM-AS-Product-Ver: IMSS-7.0.0.3127-5.5.0.1026-16438.00
    10. X-TM-AS-Result: No-2.664-4.5-31-1
    11. .......
    To copy to clipboard, switch view to plain text mode 

    I grab the "Date:.." part, clean unnecessary things...
    I put day to day(in number format), month to month(that is character format), year to year..The actual code is:

    Qt Code:
    1. QString dayremoved;
    2. QStringList datelist;
    3. dayremoved= dateHeader.section(",",1,1);
    4. datelist = dayremoved.split(" ");
    5. QString day=datelist[1];
    6. QString month=datelist[2];
    7. QString year =datelist[3];
    8. QDate todate= QDate::fromString(day+"/"+month+"/"+year,"dd/MMM/yyyy");
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QDate::fromString(input, "dd MMM yyyy");
    To copy to clipboard, switch view to plain text mode 
    also didn't work?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting QDate from string

    note that "dd" expects leading zeros (01, 02, ...) whereas "d" does not (1, 2, ...).

  5. The following user says thank you to caduel for this useful post:

    yagabey (1st February 2009)

  6. #5
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting QDate from string

    Yes you've go it It works now...!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting QDate from string

    Correct me if I'm wrong but this is basically the Qt::TextDate format, isn't it?

  8. #7
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting QDate from string

    yes it is...

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting QDate from string

    So why not simply use:
    Qt Code:
    1. QDateTime::fromString(input, Qt::TextDate)
    To copy to clipboard, switch view to plain text mode 
    ? The only possible problem is the time zone at the end of the string.

  10. #9
    Join Date
    Jan 2009
    Location
    Italy
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Getting QDate from string

    I've a similar problem. I need to convert 24-10-1977 (and 24/10/1977) to 1977-10-24

    I've tried with:

    QString fdate = value.value<QString>();
    QDate qdate = QDate::fromString(fdate,"dd-mm-yyyy");

    qDebug() << qdate.toString(Qt::ISODate);

    the last row output is null.

    It doesn't work. Any idea?

  11. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting QDate from string

    what will you get if you do this?
    Qt Code:
    1. ....
    2. qDebug() << qicssmtpmailer;
    3. ....
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #11
    Join Date
    Jan 2009
    Location
    Italy
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Getting QDate from string

    solve. The correct line is:

    QDate qdate = QDate::fromString(fdate,"d-M-yyyy");

Similar Threads

  1. String operations, printing to stdout
    By Cruz in forum Newbie
    Replies: 3
    Last Post: 20th January 2009, 15:30
  2. Problems with QDate
    By cyberboy in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2008, 21:17
  3. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  4. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  5. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38

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.