Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML date and time manipulation
Forum Updated to NodeBB v4.3 + New Features

QML date and time manipulation

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 6 Posters 61.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ad5xj
    wrote on last edited by
    #1

    There seems to be no straightforward date and time manipulation / conversion facility as in Qt C++.

    My project requires me to get the current date and find out what the current month day and year are in the "yyMMdd" format using the current date/time. I find there not a well documented indication as to how to go about this even though I would think it is a common task.

    The QML documentation says to use Qt.formatDateTime(datetime date, format variant) to return a string, which seems to be a reformat of the C++ QString formatting functions.

    Well the problem is there is no straightforward way indicated in the docs to obtain the current datetime to format as available with QDate or QDateTime currentDate(). I have tried to use the date variable type and update it to Date.now but that is the wrong data type for formatDateTime(). The javascripte Date() and Date().now are strings not datetime data types.

    There are apparent holes in the documentation on date time functions in QML / Quick / JavaScript? Can someone help a Qt C++ programmer and newcomer to QML?

    Ken AD5XJ

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kxyu
      wrote on last edited by
      #2

      use Javascript's date
      @
      var today = new Date() // contains current date and time
      @

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        Hi,

        As Kxyu mentions, you should be able to use the JS Date object (it's internally converted to a QDateTime). For example, you can display a formatted date like this:

        @
        import Qt 4.7

        Rectangle {
        width: 200
        height: 200
        Text {
        anchors.centerIn: parent
        text: Qt.formatDateTime(new Date(), "yyMMdd")
        }
        }
        @

        Regards,
        Michael

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ad5xj
          wrote on last edited by
          #4

          Since I wanted to manipulate the Date() value and not the string I had to construct a javascript function to do just that. QML does not expose all the methods of QDateTime.

          @
          function getYesterday( ) {
          var today = new Date();
          var dateTime = new Date( Qt.formatDate(today, "yy/MM/dd") );

          dateTime.setMinutes( Qt.formatDateTime ( today, "mm" )  );
          dateTime.setHours( Qt.formatDateTime ( today, "hh" )-24 );
          return Qt.formatDate(dateTime, "yyMMdd");
          

          }
          @
          The string returned is the value needed.
          I would have preferred to do it all in QML the way C++ makes these functions available.

          Ken AD5XJ

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kxyu
            wrote on last edited by
            #5

            man, you're insane or something. you function returns crap
            I even don't know, if I understood you right, but this returns yesterday's date like yyMMdd

            @
            function getYesterday( ) {
            var today = new Date();
            today.setDate(today.getDate()-1)
            return Qt.formatDate(today, "yyMMdd");
            }

            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Please don't be offending. We all want to be treated respectful.

              Thanks

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              2
              • A Offline
                A Offline
                ad5xj
                wrote on last edited by
                #7

                Yes you are right. The function DOES return the date in the "yyMMdd" format. That is because it is part of a URL string that requires it, not because it is used in a normal date string.

                Ken AD5XJ

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sree7k7
                  wrote on last edited by
                  #8

                  hi guys,

                  i got a problem with time format code,
                  for example: i would like to display Asian, European timings.
                  Is their any code, please give me the syntax to display any country timings.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    chrisadams
                    wrote on last edited by
                    #9

                    Hi,

                    Take a look at QLocale. You might need to create a helper QML type with a Q_INVOKABLE function with a signature like: QString formattedDateForLocale(const QDateTime &dt, const QString &localeIdentifier).

                    Cheers,
                    Chris.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sree7k7
                      wrote on last edited by
                      #10

                      hey chris,

                      Well, i am interested in javascript. i think, for your suggestions need c++.

                      can please fetch it in javascript, if have any related tutorial, i would be more happy for your concern. I am a newbie to QML.

                      /sree7k7

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ad5xj
                        wrote on last edited by
                        #11

                        Qt5 and javascript are very easy to use now. If you have not upgraded, now is the time. See the Qt5 Docs.

                        Ken AD5XJ

                        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