⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

[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.

  • "QDate::fromString()":http://doc.qt.digia.com/qt/qdate.html#fromString-2

    @QString str("11/11/2011");
    QDate converted_date(QDate::fromString(str, "dd/MM/yyyy"));@

    Watch the case MM is months while mm is minutes

  • 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]

  • Thanks Nosf, it the closest I'm going to get to a solution.

    The input format isn't constant.

    The user get to choose the format, but I don't know what formats that can be.

    But for now it's 'solved'.


  • If it's solved, be sure and edit your initial post and change the title to add [Solved]. Thanks!

Log in to reply