# Compare Actual Time with Node-Red Time

**URL:** <https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719>\
**Category:** General\
**Tags:** function-node\
**Created:** [19 April 2023 16:37 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719 "2023-04-19T16:37:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [19 April 2023 16:37 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/1 "2023-04-19T16:37:31Z")

</div>

Hi all,

is it possible to compare the actual time to a given time and act accordingly ?  
The code that I already have looks like this, but I do not how know to get the actual time from node-red and compare it to a planned time:

```auto
IF (pump_status == "ZP1"){
    IF (06:00) set switch_Status = "ON";
    IF (06:02) set switch_Status = "OFF";
    IF (07:00) set switch_Status = "ON";
    IF (07:00) set switch_Status = "OFF";
}

IF(pump_status == "ZP2"){
    IF(09: 00) set switch_Status = "ON";
    IF(09: 05) set switch_Status = "OFF";
    IF(11: 15) set switch_Status = "ON";
    IF(11: 22) set switch_Status = "OFF";
}

if (switch_status == "ON" && pump_status == "OFF") return [msg,null]; // Turn On
if (switch_status == "OFF" && pump_status == "ON") return [null,msg]; // Turn Off

```

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [19 April 2023 18:52 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/2 "2023-04-19T18:52:03Z")

</div>

You can get the current local time in Node-RED as a string with a function node containing the following:

```auto
msg.payload = time();
function time() { return { time: new Date().toLocaleTimeString() } };
return msg;

```

You can read more about the JavaScript Date object here: [Date - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 April 2023 18:53 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/3 "2023-04-19T18:53:41Z")

</div>

> [@Gawan](#):
>
> is it possible to compare the actual time to a given time and act accordingly ?

Yes, of course.

However, you will find it a LOT easier if you use one of the existing contributed nodes that will do this comparison for you. For example, the CRON+ node lets you set outputs based on time of day as do several other nodes. For example, I often use node-red-contrib-sun-position instead of node-red-contrib-cron-plus. Depends on what you find easiest and most convenient.#

But here is a more complex example from one of my lights - it is using CRON+ directly. But the sun-position nodes are used elsewhere to set wake/sleep and daylight/nighttime flags via MQTT.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/892ad6585ca99cd16cf98f62dc0f42cc12968066.png)

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [19 April 2023 20:53 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/4 "2023-04-19T20:53:27Z")

</div>

unfortunately this gives a wrong timezone

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f1e0a45d830f27a349ef430b14a1a80f7c94be5.png)

it is 22:52 right now, but the query says it is 20:52

---

<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:** [19 April 2023 21:09 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/5 "2023-04-19T21:09:33Z")

</div>

> [@Gawan](#):
>
> the query says it is 20:52

It says it is 2052 UTC (that is what the Z means) so I expect that is the right time, just displayed differently. 20:52 UTC is the same time as 22:52 UTC+2. In what format is the time that you want to compare it with? Where is that timestamp coming from?

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [19 April 2023 21:17 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/6 "2023-04-19T21:17:55Z")

</div>

Yes that could be the case.  
UTC is 2hrs behind my local time.

I want to compare with local time - right now it would be "23:16"  
The timestamp comes from new Date().toLocaleTimeString()  
The Value on top is the debugger timestamp

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [20 April 2023 02:14 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/7 "2023-04-20T02:14:14Z")

</div>

In that case use this:

```javascript
// get a timestamp
const event = new Date();
// convert to local time
msg.payload = event.toLocaleTimeString("en-US", { timeZone: "Europe/Berlin" });
return msg;

```

---

<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:** [20 April 2023 07:43 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/8 "2023-04-20T07:43:03Z")

</div>

I meant where is the time that you want to compare with the current time coming from and what format is it in?

---

<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:** [19 June 2023 07:43 UTC](https://discourse.nodered.org/t/compare-actual-time-with-node-red-time/77719/9 "2023-06-19T07:43:34Z")

</div>

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