Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QDate::fromString() dateformat restrictions.

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 20.8k Views
  • 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 Offline
    W Offline
    Woody
    wrote on last edited by
    #1

    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
    0
    • S Offline
      S Offline
      Suths
      wrote on last edited by
      #2

      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
      0
      • N Offline
        N Offline
        Nosf
        wrote on last edited by
        #3

        "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
        0
        • W Offline
          W Offline
          Woody
          wrote on last edited by
          #4

          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
          0
          • N Offline
            N Offline
            Nosf
            wrote on last edited by
            #5

            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
            0
            • F Offline
              F Offline
              flaviomarcio
              wrote on last edited by
              #6

              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
              0
              • W Offline
                W Offline
                Woody
                wrote on last edited by
                #7

                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
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved