PDA

View Full Version : Getting QDate from string



yagabey
1st February 2009, 17:13
Hello,

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


QString day=datelist[1]; //datelist contains 31 and Jan and 2009 here...
QString month=datelist[2];
QString year =datelist[3];
QDate todate= QDate::fromString(day+"/"+month+"/"+year,"dd/MMM/yyyy");

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

caduel
1st February 2009, 17:23
try

QDate::fromString(input, "dd MMM yyyy");

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

yagabey
1st February 2009, 18:03
Actually I am parsing an email header like:


....
Date: Sun, 1 Feb 2009 18:51:32 +0200
Message-ID: <c56e0c650902010851u7edd6c83p3a86fc07a9b6957b@mail. gmail.com>
Subject: konux119
From: george wi <yrtgs@gmail.com>
To: Pr Test1 <prtest1@ford.com.tr>
Content-Type: multipart/alternative;
boundary=0016364571aa18aff90461de40d2
X-TM-AS-Product-Ver: IMSS-7.0.0.3127-5.5.0.1026-16438.00
X-TM-AS-Result: No-2.664-4.5-31-1
.......

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:


QString dayremoved;
QStringList datelist;
dayremoved= dateHeader.section(",",1,1);
datelist = dayremoved.split(" ");
QString day=datelist[1];
QString month=datelist[2];
QString year =datelist[3];
QDate todate= QDate::fromString(day+"/"+month+"/"+year,"dd/MMM/yyyy");



QDate::fromString(input, "dd MMM yyyy");
also didn't work?

caduel
1st February 2009, 19:04
note that "dd" expects leading zeros (01, 02, ...) whereas "d" does not (1, 2, ...).

yagabey
1st February 2009, 19:30
Yes you've go it :) It works now...!

wysota
1st February 2009, 20:07
Correct me if I'm wrong but this is basically the Qt::TextDate format, isn't it?

yagabey
2nd February 2009, 18:56
yes it is...

wysota
3rd February 2009, 00:24
So why not simply use:
QDateTime::fromString(input, Qt::TextDate)? The only possible problem is the time zone at the end of the string.

giandrea77
17th February 2009, 15:28
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?

spirit
17th February 2009, 15:40
what will you get if you do this?


....
qDebug() << qicssmtpmailer;
....

giandrea77
17th February 2009, 15:40
solve. The correct line is:

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