# Action between two time strings

**URL:** <https://discourse.nodered.org/t/action-between-two-time-strings/70067>\
**Category:** General\
**Created:** [3 November 2022 21:13 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067 "2022-11-03T21:13:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wofforduk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wofforduk/32/64325_2.png) [@wofforduk](https://discourse.nodered.org/u/wofforduk)\
**Post date:** [3 November 2022 21:13 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067/1 "2022-11-03T21:13:45Z")

</div>

Hi There - I don't suppose there is an easy way to do this, I have 2 messages, one with a start time and one with an end time. I'd like to trigger a response/set a global message if the start and end time are within the current time.. does this make sense? My Node red skills are pretty poor but am trying!

```auto
[
    {
        "id": "14aabb1a.e5d4a5",
        "type": "tab",
        "label": "Octopus Agile",
        "disabled": false,
        "info": ""
    },
    {
        "id": "5fbec1e803d8affe",
        "type": "exec",
        "z": "14aabb1a.e5d4a5",
        "command": "python3 C:\\data\\Intelligent.py",
        "addpay": false,
        "append": "",
        "useSpawn": "true",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 800,
        "y": 300,
        "wires": [
            [
                "b746e564625d7942"
            ],
            [],
            []
        ]
    },
    {
        "id": "d9b881bcbf47a662",
        "type": "inject",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 540,
        "y": 300,
        "wires": [
            [
                "5fbec1e803d8affe"
            ]
        ]
    },
    {
        "id": "9c0e3991c5b122fb",
        "type": "debug",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1770,
        "y": 260,
        "wires": []
    },
    {
        "id": "b746e564625d7942",
        "type": "split",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "splt": ",",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 1050,
        "y": 280,
        "wires": [
            [
                "69a76f6ba6ad1639"
            ]
        ]
    },
    {
        "id": "69a76f6ba6ad1639",
        "type": "join",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 1250,
        "y": 280,
        "wires": [
            [
                "c7125c80bee630b5",
                "e655eb5323e137e4"
            ]
        ]
    },
    {
        "id": "c7125c80bee630b5",
        "type": "change",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[0]",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1440,
        "y": 260,
        "wires": [
            [
                "c308d480c5647904"
            ]
        ]
    },
    {
        "id": "c308d480c5647904",
        "type": "string",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "methods": [
            {
                "name": "between",
                "params": [
                    {
                        "type": "str",
                        "value": "\"nextRunStart\": \""
                    },
                    {
                        "type": "str",
                        "value": ":00+00:00\""
                    }
                ]
            }
        ],
        "prop": "payload",
        "propout": "payload",
        "object": "msg",
        "objectout": "msg",
        "x": 1610,
        "y": 260,
        "wires": [
            [
                "9c0e3991c5b122fb"
            ]
        ]
    },
    {
        "id": "d336940e73599754",
        "type": "debug",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1770,
        "y": 320,
        "wires": []
    },
    {
        "id": "e655eb5323e137e4",
        "type": "change",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[1]",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1440,
        "y": 320,
        "wires": [
            [
                "0535d0b676e9eac2"
            ]
        ]
    },
    {
        "id": "0535d0b676e9eac2",
        "type": "string",
        "z": "14aabb1a.e5d4a5",
        "name": "",
        "methods": [
            {
                "name": "between",
                "params": [
                    {
                        "type": "str",
                        "value": " \"nextRunEnd\": \""
                    },
                    {
                        "type": "str",
                        "value": ":00+00:00\""
                    }
                ]
            }
        ],
        "prop": "payload",
        "propout": "payload",
        "object": "msg",
        "objectout": "msg",
        "x": 1610,
        "y": 320,
        "wires": [
            [
                "d336940e73599754"
            ]
        ]
    },
    {
        "id": "d92e6fbc84e11bd3",
        "type": "inject",
        "z": "14aabb1a.e5d4a5",
        "name": "Sample returned string from script",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"nextRunStart\": \"2022-11-03 23:30:00+00:00\", \"nextRunEnd\": \"2022-11-04 06:30:00+00:00\", \"timesObj\": [{ \"startDt\": \"2022-11-03 23:30:00+00:00\", \"endDt\": \"2022-11-04 06:30:00+00:00\" }], \"updatedAt\": \"2022-11-03 21:06:45.308625+00:00\" }",
        "payloadType": "str",
        "x": 780,
        "y": 240,
        "wires": [
            [
                "b746e564625d7942"
            ]
        ]
    },
    {
        "id": "4186ce1216c2f68f",
        "type": "comment",
        "z": "14aabb1a.e5d4a5",
        "name": "Splits the string for extracting the paths",
        "info": "",
        "x": 1150,
        "y": 220,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@RavinderSingh13](https://discourse.nodered.org/u/RavinderSingh13)\
**Post date:** [4 November 2022 04:58 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067/2 "2022-11-04T04:58:01Z")

</div>

> [@wofforduk](#):
>
> I'd like to trigger a response/set a global message if the start and end time are within the current time

Could you please explain more on this one? Do you mean start and end time are same? Then trigger an action? If yes then request you to kindly do mention your node-red json code in your if possible so that we can get a glimpse of what's going on, cheers.

Thanks,  
Ravinder Singh

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 November 2022 07:42 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067/3 "2022-11-04T07:42:45Z")

</div>

@wofforduk welcome to the forum.

First your flow wouldn't import so I edited it so it would (see note at end of this post)

Looking at the data in the `inject` node, it looks like your python program is returning a json string. If you push it thru a `json` object, this is what you get:  
 ![Screen Shot 2022-11-04 at 3.17.37 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b06ac751f97433dae69bc6a2a22b1644ee37ca69.png)  
Notice all the dates are strings. To compare them you could use  
`x = Date(string_date)`  
here is a `function` node that will change all the date strings in your object to milliseconds.

```auto
let nextRunStart = msg.payload.nextRunStart
let nextRunEnd = msg.payload.nextRunEnd
let updatedAt = msg.payload.updatedAt

let startDt = msg.payload.timesObj[0].startDt
let endDt = msg.payload.timesObj[0].endDt

msg.payload.nextRunStart = Date.parse(nextRunStart)
msg.payload.nextRunEnd = Date.parse(nextRunEnd)
msg.payload.updatedAt = Date.parse(updatedAt)

msg.payload.timesObj[0].startDt = Date.parse(startDt)
msg.payload.timesObj[0].endDt = Date.parse(endDt)

return msg;

```

or you might want to edit your python code to return the values as time to eliminate the need for the conversion in your flow.

hopefully this will help you deal with the rest of your flow.

```auto
[{"id":"d92e6fbc84e11bd3","type":"inject","z":"14aabb1a.e5d4a5","name":"Sample returned string from script","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"nextRunStart\": \"2022-11-03 23:30:00+00:00\", \"nextRunEnd\": \"2022-11-04 06:30:00+00:00\", \"timesObj\": [{ \"startDt\": \"2022-11-03 23:30:00+00:00\", \"endDt\": \"2022-11-04 06:30:00+00:00\" }], \"updatedAt\": \"2022-11-03 21:06:45.308625+00:00\" }","payloadType":"str","x":380,"y":80,"wires":[["b746e564625d7942","601afc07663aa8ef","3f276b67bf9c7e76"]]},{"id":"601afc07663aa8ef","type":"debug","z":"14aabb1a.e5d4a5","name":"debug 30","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":600,"y":120,"wires":[]},{"id":"31fffee69014e9dc","type":"debug","z":"14aabb1a.e5d4a5","name":"debug 31","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":780,"y":120,"wires":[]},{"id":"cd8bc32f1068860e","type":"debug","z":"14aabb1a.e5d4a5","name":"debug 33","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":960,"y":80,"wires":[]},{"id":"3f276b67bf9c7e76","type":"json","z":"14aabb1a.e5d4a5","name":"","property":"payload","action":"","pretty":false,"x":610,"y":80,"wires":[["31fffee69014e9dc","3a3c7f285f6e9793"]]},{"id":"3a3c7f285f6e9793","type":"function","z":"14aabb1a.e5d4a5","name":"function 3","func":"let nextRunStart = msg.payload.nextRunStart\nlet nextRunEnd = msg.payload.nextRunEnd\nlet updatedAt = msg.payload.updatedAt\n\nlet startDt = msg.payload.timesObj[0].startDt\nlet endDt = msg.payload.timesObj[0].endDt\n\n\nmsg.payload.nextRunStart = Date.parse(nextRunStart)\nmsg.payload.nextRunEnd = Date.parse(nextRunEnd)\nmsg.payload.updatedAt = Date.parse(updatedAt)\n\nmsg.payload.timesObj[0].startDt = Date.parse(startDt)\nmsg.payload.timesObj[0].endDt = Date.parse(endDt)\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":80,"wires":[["cd8bc32f1068860e"]]}]

```

  

* * *
  

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)

---

<div class="post-metadata">

**Author:** ![wofforduk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wofforduk/32/64325_2.png) [@wofforduk](https://discourse.nodered.org/u/wofforduk)\
**Post date:** [10 November 2022 19:09 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067/4 "2022-11-10T19:09:58Z")

</div>

Thank you! that has certainly cleaned my flow up really well! and thanks for the tips on posting code - im a bit new here!

---

<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:** [24 November 2022 19:10 UTC](https://discourse.nodered.org/t/action-between-two-time-strings/70067/5 "2022-11-24T19:10:21Z")

</div>

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