0

Zeitspannen-Rechner, Zeitabstand zweier datetime-Felder

Ich wollte mal ein Script mit Euch teilen, welches sicher nur marginalen Alltagsnutzen hat, aber recht nett ist. Eigentlich interessierte mich nur der Zeitabstand bis zu meiner Rente und ich dachte ich würde das mal in einer Mittagspause so hinschreiben. Fehler. Ich habe dann ein altes Webinarscript (069) welches selbst minimal fehlerhaft ist und nur mit Datum funktioniert, zerpflückt und quasi neu geschrieben. Es ist leider wegen der vielen Ausnahmen riesig geworden. Falls jemand eine einfache NINOX-Funktion kennt, die die selbe Funktionalität hat, dann bitte nicht posten (oder vielleicht doch). Es hat nämlich ein Vielfaches einer Mittagspause gedauert. Ich habe viele Kontrollen mit einem Zeitspannen-Rechner durchgeführt. Mir sind keine bugs mehr aufgefallen. Um hier nicht meinen Renteneintritt zu posten, habe ich als Beispiel den Zeitabstand von der Landung der Apollo 11 bis jetzt  genommen. Nutzbar ist es für zwei Datumfelder, zwei Datetimefelder, oder ein Terminfeld. Mirko

 

"Script für die Berechnung eines Zeitabstands auf der Basis des nicht ganz korrekten Scripts 069_ZEITINTERVALL0 im Webinar";
let dateTime1 := DATETIMEFELD1oderSTART(TERMIN);
let dateTime2 := DATETIMEFELD2oderENDOF(TERMIN);
"Für die Datumsberechnung datetime in date konvertiert";
let D1 := if dateTime1 < dateTime2 then
        date(dateTime1)
    else
        date(dateTime2)
    end;
let D2 := if dateTime1 < dateTime2 then
        date(dateTime2)
    else
        date(dateTime1)
    end;
"Funktion zur Überprüfung eines Datums, ob es in einem Schaltjahr liegt";
function isLeapYear(num : number) do
    if date(num, 2, 29) != date(num, 3, 1) then
        true
    else
        false
    end
end;
"Überprüfung, ob die beiden Daten wegen der Sommer-/Winterzeit verschiedene Differenzen zu UTC besitzen";
let timeZone := number(format(dateTime1, "ZZ")) - number(format(dateTime2, "ZZ"));
let timeDuration := 1;
switch timeDuration do
case timeZone = -100:
    (timeDuration := number(time(24, 0) - time(dateTime1) + time(dateTime2) - time(1, 0)))
case timeZone = 100:
    (timeDuration := number(time(24, 0) - time(dateTime1) + time(dateTime2) + time(1, 0)))
case timeZone = 0:
    (timeDuration := number(time(24, 0) - time(dateTime1) + time(dateTime2)))
end;
"Funktion als Variable zur Überprüfung ob ein Zeitabstand >= ein Tag ist.";
let isFullDayOrMore := timeDuration - 86400000 >= 0;
"Endgültige Berechnung von Stunden und Minuten im Zeitabstand";
let oneDayMs := 86400000;
let oneHourMs := 3600000;
let oneMinuteMs := 60000;
let dayMs := floor(timeDuration / oneDayMs) * oneDayMs;
let hourMs := floor((timeDuration - dayMs) / oneHourMs) * oneHourMs;
let minuteMs := floor((timeDuration - dayMs - hourMs) / oneMinuteMs) * oneMinuteMs;
let HH := hourMs / oneHourMs;
let mm := minuteMs / oneMinuteMs;
if D1 = null or D2 = null then
    ""
else
    if D1 = D2 and timeDuration = 86400000 then
        "Anfang = Ende"
    else
        "Berechnung der Tage";
        let sameDayOfD1InMonthBeforeOfD2 := day(date(year(D2), month(D2) - 1, day(D1)));
        let lastDayOfD1 := day(date(year(D2), month(D2), 0));
        let DD := 1;
        switch DD do
        case day(D2) = day(D1) and isFullDayOrMore:
            (DD := 0)
        case day(D2) > day(D1) and isFullDayOrMore:
            (DD := day(D2) - day(D1))
        case day(D2) = day(D1) and not isFullDayOrMore:
            (DD := lastDayOfD1 - sameDayOfD1InMonthBeforeOfD2 + day(D2) - 1)
        case day(D2) > day(D1) - 1 and not isFullDayOrMore:
            (DD := day(D2) - day(D1) - 1)
        case day(D2) < day(D1) and not isFullDayOrMore and isLeapYear(year(D1)) and month(D1) = 2 and month(D2) = 3:
            (DD := 29 - if day(D1) = 29 then
                    29
                else
                    sameDayOfD1InMonthBeforeOfD2
                end + day(D2) - 1)
        case day(D2) < day(D1) and isFullDayOrMore and isLeapYear(year(D1)) and month(D1) = 2 and month(D2) = 3:
            (DD := 29 - if day(D1) = 29 then
                    29
                else
                    sameDayOfD1InMonthBeforeOfD2
                end + day(D2))
        case day(D2) < day(D1) and not isFullDayOrMore:
            (DD := lastDayOfD1 - sameDayOfD1InMonthBeforeOfD2 + day(D2) - 1)
        case day(D2) < day(D1) and isFullDayOrMore:
            (DD := lastDayOfD1 - sameDayOfD1InMonthBeforeOfD2 + day(D2))
        end;
        "Berechnung der Monate";
        let MM := 1;
        switch MM do
        case month(D1) < month(D2) and day(D1) <= day(D2) and isFullDayOrMore:
            (MM := month(D2) - month(D1))
        case month(D1) < month(D2) and day(D1) < day(D2) and not isFullDayOrMore:
            (MM := month(D2) - month(D1))
        case month(D1) < month(D2) and day(D1) > day(D2) and isFullDayOrMore:
            (MM := month(D2) - month(D1) - 1)
        case month(D1) < month(D2) and day(D1) >= day(D2) and not isFullDayOrMore:
            (MM := month(D2) - month(D1) - 1)
        case month(D1) > month(D2) and day(D1) > day(D2) and isFullDayOrMore:
            (MM := 12 - month(D1) + month(D2) - 1)
        case month(D1) > month(D2) and day(D1) >= day(D2) and not isFullDayOrMore:
            (MM := 12 - month(D1) + month(D2) - 1)
        case month(D1) > month(D2) and day(D1) <= day(D2) and isFullDayOrMore:
            (MM := 12 - month(D1) + month(D2))
        case month(D1) > month(D2) and day(D1) < day(D2) and not isFullDayOrMore:
            (MM := 12 - month(D1) + month(D2))
        case month(D1) = month(D2) and day(D1) <= day(D2) and isFullDayOrMore:
            (MM := 0)
        case month(D1) = month(D2) and day(D1) < day(D2) and not isFullDayOrMore:
            (MM := 0)
        case month(D1) = month(D2) and day(D1) > day(D2) and isFullDayOrMore:
            (MM := 11)
        case month(D1) = month(D2) and day(D1) >= day(D2) and not isFullDayOrMore:
            (MM := 11)
        end;
        "Berechnung der Jahre";
        let YY := 1;
        switch YY do
        case month(D1) < month(D2):
            (YY := year(D2) - year(D1))
        case month(D1) = month(D2) and day(D1) <= day(D2) and isFullDayOrMore:
            (YY := year(D2) - year(D1))
        case month(D1) = month(D2) and day(D1) = day(D2) and not isFullDayOrMore:
            (YY := year(D2) - year(D1) - 1)
        case month(D1) = month(D2) and day(D1) < day(D2) and not isFullDayOrMore:
            (YY := year(D2) - year(D1))
        case month(D1) = month(D2) and day(D1) > day(D2):
            (YY := year(D2) - year(D1) - 1)
        case month(D1) > month(D2):
            (YY := year(D2) - year(D1) - 1)
        end;
        "Das Anordnen der Ergebnisse.";
        if D1 = null then
            ""
        else
            if YY = 0 then
                ""
            else
                if YY = 1 then
                    YY + " Jahr" + if MM = 0 and DD = 0 and HH = 0 and mm = 0 then
                        ""
                    else
                        ", "
                    end
                else
                    YY + " Jahre" + if MM = 0 and DD = 0 and HH = 0 and mm = 0 then
                        ""
                    else
                        ", "
                    end
                end
            end + if MM = 0 then
                ""
            else
                if MM = 1 then
                    MM + " Monat" + if DD = 0 and HH = 0 and mm = 0 then
                        ""
                    else
                        ", "
                    end
                else
                    MM + " Monate" + if DD = 0 and HH = 0 and mm = 0 then
                        ""
                    else
                        ", "
                    end
                end
            end + if DD = 0 then
                ""
            else
                if DD = 1 then
                    DD + " Tag" + if HH = 0 and mm = 0 then "" else ", " end
                else
                    DD + " Tage" + if HH = 0 and mm = 0 then "" else ", " end
                end
            end + if HH = 0 then
                ""
            else
                if HH = 1 then
                    HH + " Stunde" + if mm = 0 then "" else ", " end
                else
                    HH + " Stunden" + if mm = 0 then "" else ", " end
                end
            end + if mm = 0 then
                ""
            else
                if mm = 1 then mm + " Minute" else mm + " Minuten" end
            end
        end
    end
end

4 Antworten

null
    • Master70
    • vor 5 Monaten
    • Gemeldet - anzeigen

    Hallo Mirko, 

    auch mich würde der Zeitabstand zu meiner Rente interessieren aber leider kommt es selten vor dass man das genaue Datum kennt an dem es dann soweit ist. Ich kenne zwar meine Beitragsjahre und die Jahre an denen ich ein gemeldetes Arbeitsverhältnis hatte, aber dazwischen gibt es immer wieder einige Lücken und verschiedene Arbeitgeber. Ich verwende deinen Code um die Zeit zu berechnen die ich jeweils bei meinen verschiedenen Arbeitgebern angestellt war. Deshalb bräuchte ich in einer zweiten Tabelle die Summe von allen diesen Zeitspannen. Hättest du evtl. auch dazu eine Lösung?

    Vielen Dank im Voraus  

    Andreas

      • mirko3
      • vor 5 Monaten
      • Gemeldet - anzeigen

       Hi Andreas, da das Ergebnis des Scripts einen String ergibt, läßt es sich nicht für Berechnungen verwenden. Ich lege Dir eine Datei bei, in der A. das Script als globale Funktion abgelegt ist (die Du notfalls dort kopieren kannst) und B. über einen kleinen Trick dann doch Dein Ergebnis möglich ist. Voraussetzung: Du hast zwei Datumfelder,  oder Datetimefelder namens "von" und "bis" o.ä. Da kannst Du ja mal etwas stöbern. Mirko

      • Master70
      • vor 5 Monaten
      • Gemeldet - anzeigen

      Danke  , genau so habe ich mir das vorgestellt. Ich denke du hast diese Lösung mal kurz beim Frühstück geschrieben 😉 Gruss Andreas

      • Rafael_Sanchis
      • vor 5 Monaten
      • Gemeldet - anzeigen

       Good, 😀 but I'm already retired, didn't arrive on time 👍

Content aside

  • vor 5 MonatenZuletzt aktiv
  • 4Antworten
  • 155Ansichten
  • 4 Folge bereits