Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Blockly
    5. KW aus Datum in einer Schleife möglich?

    NEWS

    • Wir empfehlen: Node.js 22.x

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker goes Matter ... Matter Adapter in Stable

    KW aus Datum in einer Schleife möglich?

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

      Kann ich in einer Schleife aus einem Datum die Kalenderwoche dieses Datums ausgeben?

      Per neuem Block "getHistory" bekomme ich den Zeitstempel, möchte das aber als KW ausgeben.
      Hat hier zufällig jemand ein Beispielblockly für mich?

      bahnuhr haus-automatisierung 2 Replies Last reply Reply Quote 0
      • bahnuhr
        bahnuhr Forum Testing Most Active @rtwl last edited by

        @rtwl

        Anbei Ermittlung per js:

        // Woche ermitteln
            var date = new Date();
            var currentThursday = new Date(date.getTime() +(3-((date.getDay()+6) % 7)) * 86400000);
            var yearOfThursday = currentThursday.getFullYear();
            var firstThursday = new Date(new Date(yearOfThursday,0,4).getTime() +(3-((new Date(yearOfThursday,0,4).getDay()+6) % 7)) * 86400000);
            var weekNumber = Math.floor(1 + 0.5 + (currentThursday.getTime() - firstThursday.getTime()) / 86400000/7);
            log(weekNumber);
        

        Blockly mach ich nicht.

        rtwl 1 Reply Last reply Reply Quote 0
        • haus-automatisierung
          haus-automatisierung Developer Most Active @rtwl last edited by

          @rtwl sagte in KW aus Datum in einer Schleife möglich?:

          bekomme ich den Zeitstempel, möchte das aber als KW ausgeben.

          Ist ein schöner Feature-Request für den JavaScript-Adapter. Nehme ich mal mit auf.

          rtwl 1 Reply Last reply Reply Quote 0
          • rtwl
            rtwl @haus-automatisierung last edited by

            @haus-automatisierung sagte in KW aus Datum in einer Schleife möglich?:

            Nehme ich mal mit auf.

            Super Danke 👍

            1 Reply Last reply Reply Quote 0
            • rtwl
              rtwl @bahnuhr last edited by

              @bahnuhr

              Vielleicht kannst du mir trotzdem helfen.
              ich habe diesen Code um aus dem aktuellen Datum die KW zu ermitteln.

              var currentDate = ts;
              currentDate.setHours(0, 0, 0, 0);
              var dayOfWeek = currentDate.getDay();
              currentDate.setDate(currentDate.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1));
              var yearStart = new Date(currentDate.getFullYear(), 0, 1);
              var weekNumber = Math.ceil((((currentDate - yearStart) / 86400000) + 1) / 7);
              return weekNumber;
              

              die Variable currentDate wird auf die Blockly-Variable ts gesetzt. In dieser ist der Timestamp aus dem History Adapter enthalten.

              Doch im Log steht dann folgendes:
              TypeError: currentDate.setHours is not a function

              Kannst du den Fehler finden?

              bahnuhr haus-automatisierung 2 Replies Last reply Reply Quote 0
              • bahnuhr
                bahnuhr Forum Testing Most Active @rtwl last edited by

                @rtwl
                Warum nimmst du nicht mein Script?
                Komisch.
                Habe es extra raus gesucht um es dir mitzuteilen.

                Musst du halt einmal vergleichen.

                rtwl 1 Reply Last reply Reply Quote 0
                • rtwl
                  rtwl @bahnuhr last edited by

                  Hab es mit deinem Skript auch probiert. Geht nicht. Ich meine es geht schon, um die aktuelle KW zu bekommen.
                  Ich bräuchte aber die KW aus dem Timestamp und dieser ist in Millisekunden anstelle des Datum-Format.

                  Bräuchte daher ein Skript wie man ein Timestamp in ein Datums-Format umwandelt, damit das Skript funktioniert.

                  1 Reply Last reply Reply Quote 0
                  • haus-automatisierung
                    haus-automatisierung Developer Most Active @rtwl last edited by haus-automatisierung

                    @rtwl sagte in KW aus Datum in einer Schleife möglich?:

                    die Variable currentDate wird auf die Blockly-Variable ts gesetzt.

                    Weil ts ein Timestamp ist. Also numerisch. Du bräuchtest quasi var currentDate = new Date(ts);

                    Aber das Script von @bahnuhr ist eh schöner (auf den ersten Blick) 🙂

                    rtwl 1 Reply Last reply Reply Quote 0
                    • rtwl
                      rtwl @haus-automatisierung last edited by

                      @haus-automatisierung
                      gut, ich werde @bahnuhr ´s Skript nehmen.
                      bei deinem Vorschlag kommt dann ReferenceError: newDate is not defined

                      Mein Testaufbau sieht so aus:
                      5271aebe-1c58-4230-97d3-731e59de28f2-image.png

                      var currentDate = newDate(ts); 
                      return currentDate;
                      
                      rtwl 1 Reply Last reply Reply Quote 0
                      • rtwl
                        rtwl @rtwl last edited by rtwl

                        @rtwl aaaaah - Leertaste vergessen 🙊

                        Danke, funktioniert

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

                        Support us

                        ioBroker
                        Community Adapters
                        Donate

                        489
                        Online

                        32.0k
                        Users

                        80.4k
                        Topics

                        1.3m
                        Posts

                        3
                        10
                        257
                        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