# Comparing times

**URL:** https://discourse.nodered.org/t/comparing-times/81368
**Category:** General
**Tags:** function-node, home-assistant
**Created:** [16 September 2023 12:34 UTC](https://discourse.nodered.org/t/comparing-times/81368 "2023-09-16T12:34:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![d\_tech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/d_tech/32/48546_2.png) [@d\_tech](https://discourse.nodered.org/u/d_tech)
#### Post date: [16 September 2023 12:34 UTC](https://discourse.nodered.org/t/comparing-times/81368/1 "2023-09-16T12:34:10Z")

</div>

Hi.  
I am trying to create a function node that determines if the current time (UTC time) falls between 2 input date time fields in home assistant.  
The current time is in UTC format, however the other 2 times are in the following format; 06:30:00 etc.  
Would anyone know how i can compare the times to determine if the current time falls between the 2 input times.  
Regards  
Danny

---

<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: [16 September 2023 14:44 UTC](https://discourse.nodered.org/t/comparing-times/81368/2 "2023-09-16T14:44:14Z")

</div>

I find this sort of time comparison easier using moment and the [isBetween()](https://momentjscom.readthedocs.io/en/latest/moment/05-query/06-is-between/) function

As you can easily enter the time format and moment will add the date portion of now.

```auto
$moment().isBetween($moment($$.payload.start, "HH:mm:ss"), $moment($$.payload.finish, "HH:mm:ss"))

```

Which should output a boolean true or false.

example flow

```auto
[{"id":"fdf121a121fc1e30","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"start\": \"06:30:00\", \"finish\": \"17:10:00\"}","payloadType":"json","x":270,"y":60,"wires":[["4c1b404fd574cf9b"]]},{"id":"4c1b404fd574cf9b","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().isBetween($moment($$.payload.start, \"HH:mm:ss\"), $moment($$.payload.finish, \"HH:mm:ss\"))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":100,"wires":[["825b1902aba22bc3"]]},{"id":"825b1902aba22bc3","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":60,"wires":[]}]

```

This is easy in a change node using JSONata, but can be done in a [function node](https://discourse.nodered.org/search?expanded=true&q=using%20moment%20in%20function) also. You can also use the expression direct in a switch node.

---

<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: [15 November 2023 14:45 UTC](https://discourse.nodered.org/t/comparing-times/81368/3 "2023-11-15T14:45:09Z")

</div>

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