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. Get current week number
Forum Updated to NodeBB v4.3 + New Features

Get current week number

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 3.3k 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.
  • S Offline
    S Offline
    schiessle
    wrote on last edited by
    #1

    Hi,

    I'm looking for a way to get the current week number. I had a look at the date object but couldn't find a method for it. Any idea how to get/calculate the week number?

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The QDate class has a method for that.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        schiessle
        wrote on last edited by
        #3

        Hi,

        thanks for the answer. How can I use QDate from within a QML script? Soemthing like "new QDate()" seems not to work.

        And the date object returns a "undefined" if I try to access weekNumber:

        var d = new Date();
        d.weekNumber;
        

        Thanks!

        ODБOïO 1 Reply Last reply
        0
        • S schiessle

          Hi,

          thanks for the answer. How can I use QDate from within a QML script? Soemthing like "new QDate()" seems not to work.

          And the date object returns a "undefined" if I try to access weekNumber:

          var d = new Date();
          d.weekNumber;
          

          Thanks!

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @schiessle hi
          that is c++ method

           QDate date(2019,03,07); 
           int weekNbr = date.weekNumber();
          

          you can do this and make weekNbr reachable from QML using setContextProperty() method:

          int main(int argc, char *argv[])
          {
              QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              QGuiApplication app(argc, argv);
              QQmlApplicationEngine engine;
          
               QDate date(2019,03,07);  
               int weekNbr = date.weekNumber();
          
               engine.rootContext()->setContextProperty("weekNbr",weekNbr);
          
          //use it in QML
          
          Component.onCompleted: {console.log("weekNbr : " + weekNbr)}
          

          or full js : (source https://weeknumber.net/how-to/javascript)

          Component.onCompleted: {
              var date = new Date();
                date.setHours(0, 0, 0, 0);
                // Thursday in current week decides the year.
                date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
                // January 4 is always in week 1.
                var week1 = new Date(date.getFullYear(), 0, 4);
                // Adjust to Thursday in week 1 and count number of weeks from date to week1.
          
              console.log(1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                                          - 3 + (week1.getDay() + 6) % 7) / 7))
          }
          
          hskoglundH 1 Reply Last reply
          1
          • S Offline
            S Offline
            schiessle
            wrote on last edited by
            #5

            @LeLev Thanks for the detailed example and the link to the JavaScript example.

            I chosed the JavaScript way and it works nicely!

            Background: I use a Qt program which allows to write "plugins" as QML scripts. I want to write such a plugin. Therefore I need a way to get the week number without patching the main program to expose the week number. That's why I choosed the JS way.

            1 Reply Last reply
            0
            • ODБOïO ODБOï

              @schiessle hi
              that is c++ method

               QDate date(2019,03,07); 
               int weekNbr = date.weekNumber();
              

              you can do this and make weekNbr reachable from QML using setContextProperty() method:

              int main(int argc, char *argv[])
              {
                  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                  QGuiApplication app(argc, argv);
                  QQmlApplicationEngine engine;
              
                   QDate date(2019,03,07);  
                   int weekNbr = date.weekNumber();
              
                   engine.rootContext()->setContextProperty("weekNbr",weekNbr);
              
              //use it in QML
              
              Component.onCompleted: {console.log("weekNbr : " + weekNbr)}
              

              or full js : (source https://weeknumber.net/how-to/javascript)

              Component.onCompleted: {
                  var date = new Date();
                    date.setHours(0, 0, 0, 0);
                    // Thursday in current week decides the year.
                    date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
                    // January 4 is always in week 1.
                    var week1 = new Date(date.getFullYear(), 0, 4);
                    // Adjust to Thursday in week 1 and count number of weeks from date to week1.
              
                  console.log(1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                                              - 3 + (week1.getDay() + 6) % 7) / 7))
              }
              
              hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              @LeLev Careful with those js libraries, if you want valid week numbers on the year 2029 or later, maybe choose another library. It just did a quick test (someone please verify)
              0_1552082185598_Screenshot from 2019-03-08 22-42-00.png
              0_1552082194231_Screenshot from 2019-03-08 22-52-32.png 0_1552082201824_Screenshot from 2019-03-08 22-53-19.png
              The first difference I found is on New Year's Eve 2029 (see first picture, screen shot of my Ubuntu 18.04)

              2nd picture is a dump of weeknumber using that javascript code, ranging from December 28 to January 4 the following year (i.e. 8 days in a row).

              3rd picture is a similar dump using QDate.weekNumber();
              (Also New Year's Eve 2035 differs)

              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