fix: URLs für Datum und Uhrzeit angepasst bei Systemzeit übernehmen Button, führende Nullen entfernt

- Die Funktion `handleSetDateTime` wurde angepasst, um das URL-Format den Anforderungen entsprechend zu gestalten.
- Monat, Tag, Stunden, Minuten und Sekunden werden jetzt ohne führende Nullen dargestellt, um eine konsistentere Darstellung zu gewährleisten.
- Die URL wird nun korrekt formatiert, z.B. `https://10.10.0.118/CPL?/dashboard.html&CLK00=24-11-8&CLK01=14-3-5` statt `https://10.10.0.118/CPL?/dashboard.html&CLK00=24-11-08&CLK01=14-03-05`.
- Implementierung mit `Number()` zur Umwandlung und Entfernung der führenden Nullen.

Diese Änderung verbessert die Lesbarkeit und die Kompatibilität des URL-Formats mit dem Zielsystem.
This commit is contained in:
ISA
2024-11-08 14:56:20 +01:00
parent aa9ff6ea24
commit de1c94a6a2

View File

@@ -131,40 +131,38 @@ function SettingModal({ showModal, onClose }) {
const handleSetDateTime = () => {
const currentDate = new Date();
// Format date and time as required by the system:
// Format date and time as required by the system without leading zeros:
const year = currentDate.getFullYear().toString().slice(-2); // Last two digits of the year
const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // Month (1-12)
const day = String(currentDate.getDate()).padStart(2, "0"); // Day (1-31)
const month = Number(currentDate.getMonth() + 1); // Convert to Number to remove leading zero
const day = Number(currentDate.getDate()); // Convert to Number to remove leading zero
const hours = String(currentDate.getHours()).padStart(2, "0"); // Hours (0-23)
const minutes = String(currentDate.getMinutes()).padStart(2, "0"); // Minutes (0-59)
const seconds = String(currentDate.getSeconds()).padStart(2, "0"); // Seconds (0-59)
const hours = Number(currentDate.getHours()); // Convert to Number to remove leading zero
const minutes = Number(currentDate.getMinutes()); // Convert to Number to remove leading zero
const seconds = Number(currentDate.getSeconds()); // Convert to Number to remove leading zero
// Date command
// Date and Time commands
const dateCommand = `CLK00=${year}-${month}-${day}`;
// Time command
const timeCommand = `CLK01=${hours}-${minutes}-${seconds}`;
// Log the URLs to the console
console.log(`/CPL?${dateCommand}`);
console.log(`/CPL?${timeCommand}`);
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, date, and time commands
const url = `${window.location.origin}/CPL?${currentPath}&${dateCommand}&${timeCommand}`;
// Log the full URL to the console
console.log(url);
// Send the commands to the server using fetch and GET method
fetch(`/CPL?${dateCommand}`, { method: "GET" })
fetch(url, { method: "GET" })
.then((response) => {
if (response.ok) {
alert("Datum erfolgreich gesetzt!");
return fetch(`/CPL?${timeCommand}`, { method: "GET" });
alert("Datum und Uhrzeit erfolgreich gesetzt!");
} else {
alert("Fehler beim Setzen des Datums!");
throw new Error("Date setting failed.");
}
})
.then((response) => {
if (response.ok) {
alert("Uhrzeit erfolgreich gesetzt!");
} else {
alert("Fehler beim Setzen der Uhrzeit!");
alert("Fehler beim Setzen von Datum und Uhrzeit!");
}
})
.catch((error) => {
@@ -172,6 +170,7 @@ function SettingModal({ showModal, onClose }) {
alert("Fehler beim Setzen von Datum und Uhrzeit!");
});
};
//---------------------------------------------------
// Setze initiale Werte nur beim Öffnen des Modals
useEffect(() => {