Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. day of year

    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

    day of year

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

      Hallo ihr alle da draußen

      Ich wollte den Tag im Jahr ausrechnen mit folgendem Code:

      function tag_im_jahr(){
          let now = new Date();
          let start = new Date(now.getFullYear(), 0, 0);
          let diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
          let oneDay = 1000 * 60 * 60 * 24;
          let day = Math.floor(diff / oneDay);
      return day;
      }
      

      So weit so gut es funktioniert auch aber die variablen "now" und "start" werden rot unterstrichen mit folgendem Fehler:

      let now: Date
      The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)
      
      let start: Date
      The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2363)
      

      Wie bekomme ich den Skript sauber??

      David G. paul53 2 Replies Last reply Reply Quote 0
      • David G.
        David G. @Pat last edited by David G.

        @pat

        Ich rechne den Doy so aus.
        Klappt wunderbar.

        Screenshot_20210801-223449_Chrome.jpg

        So sieht es in der Konsole aus
        Screenshot_20210801-223715_RaspController.jpg

        1 Reply Last reply Reply Quote 0
        • paul53
          paul53 @Pat last edited by paul53

          @pat sagte: Wie bekomme ich den Skript sauber??

          Javascript macht zwar eine implizite Typwandlung, aber korrekt ist es so:

          now.getTime() - start.getTime()
          

          Ich würde es so machen, da es sich besser liest:

          function tag_im_jahr(){
              let now = new Date();
              let start = new Date(now.getFullYear() + '-01-01 00:00');
              let diff = now.getTime() - start.getTime() + 60000 * (start.getTimezoneOffset() - now.getTimezoneOffset());
              return Math.ceil(diff / 86400000);
          }
          
          P 1 Reply Last reply Reply Quote 1
          • P
            Pat @paul53 last edited by

            @paul53 said in day of year:

            Vielen lieben dank dir

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

            Support us

            ioBroker
            Community Adapters
            Donate

            821
            Online

            31.8k
            Users

            80.0k
            Topics

            1.3m
            Posts

            3
            4
            302
            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