/* ************************************************************************************ * Das Modul beinhaltet Funktionen zur Berechnung bzw. Konvertierung von Datum und * Uhrzeiten, Schaltjahr, Führende Nullen. * Die Funktionen und Algorithmen haben keinen Anspruch auf * hohe Genauigkeit, Vollständigkeit und Richtigkeit. * * Funktionsliste: * * - function convertDateToDaycount (day, month, year) * - function isLeapYear(year) * - function LPad( iZahl, iAnzStellen, sFuellzeichen ) * - function dectime2Time(dectime, what) * - function time2Dectime(HH, MM, SS, MS) * * * 21.09.2020, Dieter Steffan * *********************************************************************************** */ /* ****************************************************** Die Funktion ermittelt die Anzahl der Tage des angegebenen Datums von Jahresanfang bis zu dem Datum. Dabei werden Schaltjahre berücksichtigt. Es wird keine Plausibilitätsprüfung der Übergabedaten vor- genommen. Übergabewert: day = Integer und der Tag des Datums month = Integer und der Monat des Datums year = Integer und das Jahr des Datums Rückgabewert: Anzahl der Tage des Jahres bis zu dem Datum als Integer. ---------------------------------------------------- Date from the beginning of the year to the date. This involves: Schedule years are taken into account. No verification of the plausibility of the surrender data is provided. taken. Transmission value: day = integer and date day month = integer and the month of the date year = integer and the year of the date Return value: Number of days of the year up to the date as integer. *************************************************** */ function convertDateToDaycount (day, month, year){ var DAY = parseInt(day); // Den Tag des Monats Übernehmen var MONTH = parseInt(month); // Den Monat Übernehmen var YEAR = parseInt(year); // Das Jahr Übernehmen var daysTillDate = 0.0; // Returnwert ist Dezimalzahl // Für Monate grösser Februar if (MONTH > 2){ // Addiere 1 zum Monat hinzu ... // Add 1 to the month . . . MONTH += 1; // ... und multipliziere mit 30,6 ... // . . . and multiply by 30,6 . . . daysTillDate = MONTH * 30.6; // ... nehme den ganzzahligen Anteil davon ... //. . . take the integer part of it ... daysTillDate = Math.floor(daysTillDate); // Ist das Jahr ein Schaltjahr ... // Is the year a leap year . . . if (isLeapYear(YEAR)){ // ... dann subtrahiere 62 ... // . . . then subtract 62 . . . daysTillDate = daysTillDate - 62; // Kein Schaltjahr ... // No leap year . . . } else { // ... und bei normalem Jahr 63 // ... and in normal year 63 daysTillDate = daysTillDate - 63; } // ... und dazu kommt der Tag des Monats. // Dies ist die Anzahl der Tage des Jahres bis zum Datum. // . . . and on top of that comes the day of the month. // This is the number of days of the year up to the date. daysTillDate = parseInt(daysTillDate) + parseInt(DAY); // Für die Monate Januar und Februar // For the months of January and February } else { // Subtrahiere 1 vom Monat ... // Subtract 1 from the month . . . MONTH -= 1; // Ist das Jahr ein Schaltjahr ... // Is the year a leap year . . . if (isLeapYear(YEAR)){ // ... dann multipliziere den Monat // mit 62 und dividiere durch 2 ... //. . . then multiply the month // with 62 and divide by 2 . . . daysTillDate = (MONTH * 62) / 2; // Kein Schaltjahr ... // no Leap Year ... } else { // ... dann multipliziere den Monat // mit 63 und dividiere durch 2 ... //. . . then multiply the month // with 63 and divide by 2 . . . daysTillDate = (MONTH * 63) / 2; } // ... und davon den ganzzahligen Anteil ... // ... and the integer part of it ... daysTillDate = Math.floor(daysTillDate); // ... und dazu die Tage des Monats und das ist die Anzahl // der Tage im Jahr bis zu dem Datum ... // . . . and the days of the month and that's the number // the days in the year up to the date . . . daysTillDate = parseInt(daysTillDate) + parseInt(DAY); } // und Wert an den Caller zurückgeben. // and return the value to the caller. return(daysTillDate); } /* ****************************************************** Die Funktion ermittelt ob ein Jahr ein Schaltjahr ist. Ein Schaltjahr liegt vor, wenn das Jahr durch 4 und nicht durch 100 teilbar ist. Ist ein Jahr durch 4 und durch 100 teilbar, dann muss es durch 400 teilbar sein, um ein Schaltjahr zu sein. Übergabewert: year = Ganzzahl, z.B. 1988 Rückgabewert: true = Schaltjahr false = Kein Schaltjahr ---------------------------------------------------- The function determines whether a year is a leap year. A leap year is given if the year by 4 and not is divisible by 100. If a year is divisible by 4 and by 100, then it must be divisible by 400 to add one leap year. Transfer value: year = integer, e. g. B. 1988 Return value: true = leap year false = No leap year *************************************************** */ function isLeapYear(year){ ret = false; var fourhundred = Math.floor(year / 400.0); // Ganzzahl der 400 - Entire number of 400 var yearFourhundred = year / 400.0 // Dezimalzahl der 400 - Decimal number of 400 var four = Math.floor(year / 4.0); // Ganzahl der 4 - Integer of the 4 var yearFour = year / 4.0; // Dezimalzahl der 4 - Decimal number of 4 var hundred = Math.floor(year / 100.0); // Ganzzahl 100 - Integer of the 100 var yearHundred = year / 100.0; // Dezimalzahl 100 - Decimal number of 100 // Ist das Jahr durch 4 und nicht durch 100 teilbar, dann // Schaltjahr ... // If the year is divisible by 4 and not by 100, then // Leap year. . . if ( ((yearHundred - hundred) != 0) && ( (yearFour - four) == 0) ){ ret = true; // Ist es durch 4 und durch 100 teilbar, muss es durch // 400 teilbar sein. // If it is divisible by 4 and by 100, it must be // be divisible. } else if ( (yearFourhundred - fourhundred) == 0 ) { ret = true; } // ... und zurück zum Caller. // ... and back to the caller. return(ret); } /* ****************************************************** Die Funktion füllt eine Zeichenketten mit Zahl vorne mit n-Zeichen auf. Z.B. 1 -> 01 izahl = Die Zahl ohne führende Nullen ianzStellen = Die gewünschte Anzahl von Stellen sFuelzeichen = Das gewünschet führende Zeichen Rückgabewert = Z.B. "01" oder "0001" ---------------------------------------------------- The function fills a string with number at the front with n-signs. E. g. 1 -> 01 izahl = The number without leading zeros ianzStellen = The desired number of digits sFuel character = The desired leading character Return value = E. g. "01" or "0001" *************************************************** */ function LPad( iZahl, iAnzStellen, sFuellzeichen ){ // Die aufzufüllende Zahl als Zeichenkette Deklarieren. // Declare the number to be filled as a string. var sZahl = iZahl + ""; // Vorne immer ein Füllzeichen anfügen. // Always add a fill character to the front. while( sZahl.length < iAnzStellen ) sZahl = sFuellzeichen + sZahl; // ... und zurück zum Caller. // ... and back to the caller. return sZahl; } /* ******************************************************* Die Funktion konvertiert eine dezimale Stundenangabe, wie z.B. 18,5421666 in eine Stundenangabe hh:mm.ss.ms oder ein Array mit den einzelnen Werten. Die einzelnen Werte des Rückgabewertes werden links mit ein "0" aufgefüllt, wenn der Wert < 10 ist. Übergabewert: dectime = Stundenwert als Dezimalzahl what = Was zurückgegeben werden soll, ein String = "hh:mm:ss.ms" oder ein Array mit Index von 0 - 4 der einzelnen Werte. Wert: "array" oder "string" Returnwert: String z.B. als 18:01:45:005 oder Array mit 4 Werten an den einzelnen Indexen von 0 - 4 für hh mm ss ms ------------------------------------------------------- The function converts a decimal number of hours, as e. g. e. g. 18,5421666 into an hourly indication hh:mm.ss.ms or an array with the individual values. The individual values of the return value are left filled with a “0”, if the value is < 10. Transfer value: dectime = hour value as decimal number what = What should be returned, a string = “hh:mm:ss.ms” or an array with an index of 0-4 of the individual values. Value: “array” or “string” Return value: String z. e. g. as 18:01:45:005 or Array with 4 values on the individual indexes from 0-4 for hh mm ss ms. ****************************************************** */ function dectime2Time(dectime, what){ // Deklaration und Definition der Variablen. // Declaration and definition of the variables. var rest = 0.0; var hour = 0; var minute = 0; var second = 0; var msecond = 0; // Ermittle was der Rückgabewert sein soll? ... // Determine what the return value should be? ... var WHAT = what.toLowerCase(); // ... ein Array mit den einzelnen Werten von h, m, s, ms ... // ... an array with the individual values of h, m, s, ms ... if (WHAT == "array") var time = []; // ... oder eine zusammengesetzte Zeichenkette wie hh:mm:ss.ms. // . . . or a compound string like hh:mm:ss. ms. else var time = ""; // Stunden berechnen ... // Calculate hours... hour = Math.floor(dectime * 24.0); // ... und die Nachkommawerte ... // ... and the decimals ... rest = (dectime * 24.0) - hour; // ... und Stunden zum Ergebnis hinzufügen ... // ... and add hours to the result ... if (WHAT == "array") time.push(LPad(hour, 2, "0")); else time += LPad(hour, 2, "0") + ":"; // Die Minuten berechnen ... // Calculate the minutes ... minute = Math.floor(rest * 60.0); // ... und deren Nachkommastellen ... // ... and their decimal places ... rest = (rest * 60.0) - minute; // ... und zum Ergebnis hinzufügen ... // ... and add to the result ... if (WHAT == "array") time.push(LPad(minute, 2, "0")); else time += LPad(minute, 2, "0") + ":"; // Die Sekunden berechnen // Calculate the seconds ... second = Math.floor(rest * 60.0); // ... und deren Nachkommastellen ... // ... and their decimal places ... rest = (rest * 60.0) - second; // ... und zum Ergebnis hinzufügen ... // ... and add to the result ... if (WHAT == "array") time.push(LPad(second, 2, "0")); else time += LPad(second, 2, "0") + "."; // für die Milli-Sekundenberechnung ist ... // for the milli-second calculation is . . . msecond = Math.trunc(rest * 1000.0); // ... und zum Ergebnis hinzufügen ... // ... and add to the result ... if (WHAT == "array") time.push(LPad(msecond, 3, "0")); else time += LPad(msecond, 3, "0"); return(time); } /* ******************************************************* Die Funktion konvertiert die vier zu übergebenden Zeit- werte in eine Gleitkommazahl vom Typ Number. Übergabewerte als 24h Zeitdaten: HH = Stunden als Ganzahl MM = Minuten als Ganzzahl SS = Sekunden als Ganzzahl MS = Millisekunden als Ganzzahl Returnwert: Stundenwert als Decimalzahl ------------------------------------------------------- The function converts the four time- values to a floating point number of type Number. Transfer values as 24h Time data: HH = hours as number of hours MM = minutes as an integer SS = seconds as an integer MS = milliseconds as an integer Return value: Hour value as decimal number ****************************************************** */ function time2Dectime(HH, MM, SS, MS){ // Initialisierung der Variablen // Initialization of the variables var dectime = 0.0; var val = 0.0; var hh = HH; var mm = MM; var ss = SS; var ms = MS; // Die Millisekunden : 1000, da 1 Sek. = 1000 ms ... // The milliseconds: 1000, da 1 sec. = 1000 ms ... val = Number(ms) / 1000.0; // ... und zu den Sekunden addieren ... // ... and add to the seconds ... ss = (Number(ss) + val); // ... die Sek. : 60, da 1 Minute = 60 Sek. ... // ... the second. 60. by 1 minute = 60 seconds. ... val = Number(ss) / 60.0; // ... und dann zu den Minuten addieren ... // ... and then add to the minutes ... mm = (Number(mm) + val); // Die Minuten : 60, da 1 Stunden = 60 Minuten ... // the minutes: 60, since 1 hours = 60 minutes ... val = (Number(mm)) / 60.0; // ... und dann nur noch die Stunden dazu addieren ... // ... and then only add the hours to it ... dectime = (val + Number(hh)); // ... und zurück an den Caller. // ... and back to the caller. return(dectime); }