[Solved] QDate::fromString() dateformat restrictions.
-
I want to parse an incomming text to a date format.
But since I live in Belgium, people often use format dd/mm/yyyy.
It seems that Qt, can't handle this kind of date.
Are there any restrictings on QDate::fromString()?
If yes, please tell me where I can find them.Thanks in advance.
-
Yes it can you need to tell it the format though so something like QDate::fromString("22112012", "dMyyyy"); that should do the trick read here http://doc.qt.digia.com/qt/qdate.html#fromString for further info.
-
I think I phrased my question wrong.
This is what a want to achieve:
@birthdate = (QDate::fromString(23/11/2012, "yyyy-MM-dd").isValid() ? QDate::fromString(23/11/2012, "yyyy-MM-dd") : QDate(1970,01,01));@Birthdate has to be the format yyyy-MM-dd
So I get an inputdate of dd/MM/yyyy and I want to get yyyy-MM-dd into my database.Is it possible to be not possible? Do you have to split the date yourself?
-
is the input format constant?
if so just use
@QDate::fromString(variable, "dd/MM/yyyy");@
If you don't there isn't a foolproof way of getting it correctly. If this is an input from the user use a validator to validate their input and check the format is right or do a string replace with regex
@variable = variable.replace(QRegExp("[/, ]"), "-");
QDate::fromString(variable, "dd-MM-yyyy");@ -
I have it and it worked well
@
QString Tsysfun::SSQLDat(QDateTime v)
{
const sformat="yyyy-MM-dd hh:mm:ss";
return QString("'%1'").arg(QDateTime::fromString(v.toString(),Qt::TextDate).toString(sformat));
}@[Edit: Added @ tags to code -- mlong]