Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    [Solved] QDate::fromString() dateformat restrictions.

    General and Desktop
    5
    8
    20018
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      Woody last edited by

      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.

      File not found. Nobody leave the room!

      1 Reply Last reply Reply Quote 0
      • S
        Suths last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • N
          Nosf last edited by

          "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

          1 Reply Last reply Reply Quote 0
          • W
            Woody last edited by

            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?

            File not found. Nobody leave the room!

            1 Reply Last reply Reply Quote 0
            • N
              Nosf last edited by

              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");@

              1 Reply Last reply Reply Quote 0
              • F
                flaviomarcio last edited by

                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]

                Flavio Portela

                1 Reply Last reply Reply Quote 0
                • W
                  Woody last edited by

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

                  Thanks!

                  File not found. Nobody leave the room!

                  1 Reply Last reply Reply Quote 0
                  • M
                    mlong last edited by

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

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post