# Date Format wrong?

**URL:** <https://discourse.nodered.org/t/date-format-wrong/94711>\
**Category:** General\
**Created:** [18 January 2025 11:11 UTC](https://discourse.nodered.org/t/date-format-wrong/94711 "2025-01-18T11:11:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![firstfacility](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@firstfacility](https://discourse.nodered.org/u/firstfacility)\
**Post date:** [18 January 2025 11:11 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/1 "2025-01-18T11:11:07Z")

</div>

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 ?

```auto
// Ü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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 January 2025 11:32 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/2 "2025-01-18T11:32:43Z")

</div>

where do the variables `sleepStart` and `sleepEnd` come from?

they are not declared in your code.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 January 2025 11:37 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/3 "2025-01-18T11:37:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![firstfacility](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@firstfacility](https://discourse.nodered.org/u/firstfacility)\
**Post date:** [18 January 2025 12:36 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/4 "2025-01-18T12:36:42Z")

</div>

here's the flow:

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 January 2025 13:24 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/5 "2025-01-18T13:24:26Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

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

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

To the issue

```auto
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.

---

<div class="post-metadata">

**Author:** ![firstfacility](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@firstfacility](https://discourse.nodered.org/u/firstfacility)\
**Post date:** [18 January 2025 14:19 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/6 "2025-01-18T14:19:02Z")

</div>

very nice, thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 April 2025 14:19 UTC](https://discourse.nodered.org/t/date-format-wrong/94711/7 "2025-04-18T14:19:02Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
