Date Format wrong?

Hello everyone
I have a problem with my script. I'm getting errors sleepStart and SleepEnd. I guess in date format ? "const diffMs = sleepEnd - sleepStart;" Can someone help me ?

// Überprüfen, ob die Konvertierung erfolgreich war

if (isNaN(sleepStart.getTime()) || isNaN(sleepEnd.getTime())) {
    node.error("Ungültige Datumsangaben für 'sleepStart' oder 'sleepEnd'.");
    return null;
}

// Berechne die Differenz in Millisekunden und konvertiere in Stunden und Minuten
const diffMs = sleepEnd - sleepStart;
const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); // Ganze Stunden
const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60)); // Verbleibende Minuten

// Ausgabe der Differenz als z. B. "8:45"
msg.payload = `${diffHours}:${diffMinutes.toString().padStart(2, '0')}`;

// Rückgabe der Nachricht mit der berechneten Schlafdauer
return msg;

where do the variables sleepStart and sleepEnd come from?

they are not declared in your code.

It is a good idea to post a simple flow with an inject node and your function node to show the problem.

here's the flow:

Your flow file is not posted correctly and is corrupt

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

You JSON file is not a JSON and all but useless.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

To the issue

const diffMs = sleepEnd - sleepStart;

sleepEnd and sleepStart are still time objects, you have not create variables that contain the getime() integers. Try creating two new vars using sleepEnd.getTime(), and use these vars for your calculation.

very nice, thank you