# Compare time now against time as a varible

**URL:** https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435
**Category:** General
**Created:** [11 November 2022 10:26 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435 "2022-11-11T10:26:48Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 10:26 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/1 "2022-11-11T10:26:48Z")

</div>

Hi all im trying to compare 2 times and calculate the difference, one is from a trigger and the other is a pre-set time , I found how to check the times and get the time difference but i can work how to have a set time

Thanks

---

<div class="post-metadata">

### Author: ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)
#### Post date: [11 November 2022 10:38 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/2 "2022-11-11T10:38:25Z")

</div>

Have you read some doco on js dates? [JavaScript Date Set Methods](https://www.w3schools.com/js/js_date_methods_set.asp) should help you.

---

<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: [11 November 2022 11:26 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/3 "2022-11-11T11:26:08Z")

</div>

> [@gazzaman22](#):
>
> work how to have a set time

What format is the set time in?

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 12:02 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/4 "2022-11-11T12:02:16Z")

</div>

I would like it to have HH:MM

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 12:03 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/5 "2022-11-11T12:03:02Z")

</div>

Thanks I will have a look but JavaScript is new to me

---

<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: [11 November 2022 12:19 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/6 "2022-11-11T12:19:39Z")

</div>

What exactly are you asking? How to enter HH:MM in the dashboard? If not then what?

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 12:36 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/7 "2022-11-11T12:36:25Z")

</div>

Hi Colin  
I what to compare real time to a fixed time and calculate the differences , I cant work out how to create the fixed time eg 10:00:00

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 November 2022 12:41 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/8 "2022-11-11T12:41:29Z")

</div>

Do you mean (for example) "How long is it since 10 am?"  
Or "How long is it since 10 am on 6th November 2022?"

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 12:51 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/9 "2022-11-11T12:51:41Z")

</div>

How long since 10 am

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [11 November 2022 13:01 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/10 "2022-11-11T13:01:09Z")

</div>

You mean somthing like this?

```auto
let a = new Date().getHours() + 2 //the + 2 is for time zone correction
let b = 10
let c = a - b

msg.payload = c

return msg;

```

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 13:06 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/11 "2022-11-11T13:06:44Z")

</div>

Thanks I was try to over complicate it

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 November 2022 13:17 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/12 "2022-11-11T13:17:14Z")

</div>

I was thinking of this approach. Probably also overcomplicated.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fab0a04cd93b7b4b1fb8f265638b572a0b180c12.jpeg)

```auto
[
    {
        "id": "a8a266724d4a4895",
        "type": "function",
        "z": "f3e02add17e4b873",
        "name": "function 18",
        "func": "var now = new Date();\nvar tenam= new Date()\ntenam.setHours(10,0,0,0)\nvar diff = now.getTime() - tenam.getTime()\n\nmsg.payload = {\"now\": now, \"then\": tenam, \"difference (seconds)\": diff/1000}\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 60,
        "wires": [
            [
                "f7b3c244cbcdaf95"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 14:04 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/13 "2022-11-11T14:04:33Z")

</div>

thanks for the suggestion

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [11 November 2022 14:15 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/14 "2022-11-11T14:15:32Z")

</div>

Just bear in mind that 'new Date()' is z time.

When you 'tenam.setHours(12,0,0,0)' you have to add time zone. I am at +2

```auto
{"now":"2022-11-11T14:13:08.881Z","then":"2022-11-11T10:00:00.000Z","difference (seconds)":15188.881}

```

---

<div class="post-metadata">

### Author: ![gazzaman22](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gazzaman22/32/84078_2.png) [@gazzaman22](https://discourse.nodered.org/u/gazzaman22)
#### Post date: [11 November 2022 14:28 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/15 "2022-11-11T14:28:04Z")

</div>

Thanks I did spot tha,t Im in the UK so I have removed it.  
Is there a way of getting to adjust for daylight saving

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [11 November 2022 14:34 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/16 "2022-11-11T14:34:34Z")

</div>

Yes with a if statement, check if it is between the certain two dates you change the value of 'tenam.setHours(12,0,0,0)'

---

<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: [25 November 2022 14:35 UTC](https://discourse.nodered.org/t/compare-time-now-against-time-as-a-varible/70435/17 "2022-11-25T14:35:06Z")

</div>

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