Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Error/Bug
    4. Date.prototype.getWeek - Error "property 'getWeek' does not exist on type 'Date'" nach Umstellung von Node.js auf Node 8 oder auch 10

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    UNSOLVED Date.prototype.getWeek - Error "property 'getWeek' does not exist on type 'Date'" nach Umstellung von Node.js auf Node 8 oder auch 10

    This topic has been deleted. Only users with topic management privileges can see it.
    • greyhound
      greyhound last edited by

      In meinen global Skripten habe ich u. a. auch eine Funktion, die mir die Woche eines Datums zurückliefert.

      // https://stackoverflow.com/questions/7765767/show-week-number-with-javascript
      /** 
       * Get the ISO week date week number 
       */  
      Date.prototype.getWeek = function() {  
          // Create a copy of this date object  
          let target = new Date(this.valueOf());  
      
          // ISO week date weeks start on monday  
            // so correct the day number  
          let dayNr = (this.getDay() + 6) % 7;  
      
          // ISO 8601 states that week 1 is the week  
          // with the first thursday of that year.  
          // Set the target date to the thursday in the target week  
          target.setDate(target.getDate() - dayNr + 3);  
      
          // Store the millisecond value of the target date  
          let firstThursday = target.valueOf();  
      
          // Set the target to the first thursday of the year  
          // First set the target to january first  
          target.setMonth(0, 1);  
          // Not a thursday? Correct the date to the next thursday  
          if (target.getDay() != 4) {  
              target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);  
          }
      
          // The weeknumber is the number of weeks between the   
          // first thursday of the year and the thursday in the target week  
          return (1 + Math.ceil((firstThursday - target.getTime()) / 604800000)); // 604800000 = 7 * 24 * 3600 * 1000   
      };
      

      Nach der Umstellung der Umgebung auf Node v8.15.1, v10.15.3 bzw. Testweise auch v10.15.3 bekomme ich obige Fehlermeldung. Leider verwende ich diese Methode in etlichen Skripten und würde es daher in den globalen Skripten reparieren wollen.

      Hier Property 'getWeek' does not exist on type 'Date' in Typescript [duplicate] ist beispielhaft beschrieben, wie es unter Typescript gelöst werden kann, aber bei mir ist es ja (noch) javascript.

      Hat jemand gleiche/ähnliche Probleme und Tipps, wie ich es lösen könnte?

      1 Reply Last reply Reply Quote 0
      • AlCalzone
        AlCalzone Developer last edited by

        Kommt der Fehler bei der Ausführung oder im Editor?
        Falls letzteres, einfach ignorieren. Unter der Haube wird TypeScript verwendet, um die Syntax-Hilfen zu zeigen.

        Du hast die Antwort im StackOverflow-Thread selbst gepostet:

        Date is declared as a known interface by TypeScript. getWeek is not a property of Date a so it won't let you get or modify it.

        You must augment the Date interface

        Da du JS verwendest, kenne ich derzeit keinen Weg, das Interface im Skript-Adapter anzupassen.

        1 Reply Last reply Reply Quote 0
        • greyhound
          greyhound last edited by

          0635fba1-01e7-44ae-b2e4-dae4f71ff4be-image.png

          Im Editor und leider auch bei der Ausführung.
          Wenn ich meine Globalen Skripte auf TS umstelle, funktionieren diese dann weiter als Globale Skripte für die normalen JS- und TS-Skripte oder müssen die Globale Skripte dann anders eingebunden werden?

          AlCalzone 2 Replies Last reply Reply Quote 0
          • AlCalzone
            AlCalzone Developer @greyhound last edited by

            @greyhound sagte in Date.prototype.getWeek - Error "property 'getWeek' does not exist on type 'Date'" nach Umstellung von Node.js auf Node 8 oder auch 10:

            Wenn ich meine Globalen Skripte auf TS umstelle, funktionieren diese dann weiter als Globale Skripte für die normalen JS- und TS-Skripte

            Die sollten weiterhin wie gehabt funktionieren

            1 Reply Last reply Reply Quote 0
            • AlCalzone
              AlCalzone Developer @greyhound last edited by

              @greyhound sagte in Date.prototype.getWeek - Error "property 'getWeek' does not exist on type 'Date'" nach Umstellung von Node.js auf Node 8 oder auch 10:

              bei der Ausführung.

              Fehlermeldung von der Ausführung bitte. Wenn du den Code so eingebunden hast (als globales Skript), sollte es keinen Fehler geben. Außer du versuchst, ein TypeScript zu kompilieren.

              1 Reply Last reply Reply Quote 0
              • greyhound
                greyhound last edited by

                Im Globalen Skript "getWeek" wie im Screenshot, "function getDateFromISOWeek(w, y) {" ohne Fehler, reine function.

                So sieht es im Admin im Test-Skript aus:
                8381fb56-3ca8-40aa-8210-d641106f5d5b-image.png

                und hier das Log

                javascript.1	2019-05-03 21:42:28.558	info	script.js.Test.test-date: registered 0 subscriptions and 0 schedules
                javascript.1	2019-05-03 21:42:28.558	info	script.js.Test.test-date: getDateFromISOWeek:Mon Oct 28 2019 00:00:00 GMT+0100 (GMT+01:00)
                javascript.1	2019-05-03 21:42:28.558	info	script.js.Test.test-date: getWeek:22
                javascript.1	2019-05-03 21:42:28.558	info	script.js.Test.test-date: test1:122
                javascript.1	2019-05-03 21:42:28.558	info	Start javascript script.js.Test.test-date
                javascript.1	2019-05-03 21:42:28.543	info	Stop script script.js.Test.test-date
                

                okay, getWeek hatte ich bisher aber auch wie folgt verwenden "dürfen":

                getState(dppWG_circuit + nCircuitNr + '.' + idnWG_duration_week).lc.getWeek()
                

                oder

                (getState(dppWG_circuit + nCircuitNr + '.' + idnWG_duration_week).lc).getWeek()
                

                Log

                
                javascript.1	2019-05-03 21:56:38.446	error	at Script.runInContext (vm.js:130:20)
                javascript.1	2019-05-03 21:56:38.446	error	at script.js.Test.test-date:67:78
                javascript.1	2019-05-03 21:56:38.446	error	TypeError: getState(...).lc.getWeek is not a function
                javascript.1	2019-05-03 21:56:38.446	error	^
                javascript.1	2019-05-03 21:56:38.446	error	test = (getState(dppWG_circuit + nCircuitNr + '.' + idnWG_duration_week).lc).getWeek();
                javascript.1	2019-05-03 21:56:38.446	error	script.js.Test.test-date: script.js.Test.test-date:67
                javascript.1	2019-05-03 21:56:38.446	info	script.js.Test.test-date: getDateFromISOWeek:Mon Oct 28 2019 00:00:00 GMT+0100 (GMT+01:00)
                javascript.1	2019-05-03 21:56:38.446	info	script.js.Test.test-date: getWeek:22
                javascript.1	2019-05-03 21:56:38.446	info	script.js.Test.test-date: test1:122
                javascript.1	2019-05-03 21:56:38.446	info	Start javascript script.js.Test.test-date
                javascript.1	2019-05-03 21:56:35.298	info	Stop script script.js.Test.test-date
                

                Das bringt mich aber auf eine Idee ...

                test = new Date(getState(dppWG_circuit + nCircuitNr + '.' + idnWG_duration_week).lc).getWeek();
                console.log('new date(lc).getWeek:' + test);
                

                log:

                javascript.1	2019-05-03 22:04:56.879	info	script.js.Test.test-date: registered 0 subscriptions and 0 schedules
                javascript.1	2019-05-03 22:04:56.879	info	script.js.Test.test-date: new date(lc).getWeek:35
                javascript.1	2019-05-03 22:04:56.879	info	script.js.Test.test-date: getDateFromISOWeek:Mon Oct 28 2019 00:00:00 GMT+0100 (GMT+01:00)
                javascript.1	2019-05-03 22:04:56.879	info	script.js.Test.test-date: getWeek:22
                javascript.1	2019-05-03 22:04:56.879	info	script.js.Test.test-date: test1:122
                javascript.1	2019-05-03 22:04:56.879	info	Start javascript script.js.Test.test-date
                

                Das sieht für mich so aus, als ob .lc nicht mehr als Datumsobjekt erkannt wird, aber zumindest habe ich jetzt einen Workarround. Oder ist das der Fortschritt?

                Auf jeden Fall Danke, habe wieder etwas gelernt.

                AlCalzone 1 Reply Last reply Reply Quote 0
                • AlCalzone
                  AlCalzone Developer @greyhound last edited by

                  @greyhound sagte in Date.prototype.getWeek - Error "property 'getWeek' does not exist on type 'Date'" nach Umstellung von Node.js auf Node 8 oder auch 10:

                  lc nicht mehr als Datumsobjekt erkannt wird

                  lc ist ein Unix-Zeitstempel, kein Datumsobjekt. Da ist es kein Wunder, dass der Fehler kommt.

                  Dein Screnshot sieht mir so aus als werden die Typings nicht mehr richtig geladen seit der Umstellung auf 4.x - muss ich mir mal anschauen, wenn ich die Zeit habe.

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

                  Support us

                  ioBroker
                  Community Adapters
                  Donate

                  623
                  Online

                  31.8k
                  Users

                  80.0k
                  Topics

                  1.3m
                  Posts

                  date error getweek global script node.js prototype version problem
                  2
                  7
                  753
                  Loading More Posts
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes
                  Reply
                  • Reply as topic
                  Log in to reply
                  Community
                  Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                  The ioBroker Community 2014-2023
                  logo