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

QML date and time manipulation

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 6 Posters 62.6k 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.
  • 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
            3
            • 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