# Using moment.js to compare times in function node

**URL:** https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917
**Category:** General
**Tags:** home-assistant
**Created:** [26 February 2024 10:38 UTC](https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917 "2024-02-26T10:38:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lee\_evo](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@lee\_evo](https://discourse.nodered.org/u/lee_evo)
#### Post date: [26 February 2024 10:38 UTC](https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917/1 "2024-02-26T10:38:35Z")

</div>

Hi, I can't seem to work out why I always get a 'false' result when comparing 2 times in a nodered function with moment.js.

Taking the current time, and the sunset time and trying to test if it's before sunset.

appreciate any help

```auto
var sunset_time = global.get('homeassistant').homeAssistant.states["sun.sun"].attributes.next_setting;

var current_time = moment().local().format('HH:mm');
var sunset = moment.utc(sunset_time).local().format('HH:mm')

var test = moment(current_time).isBefore(moment(sunset))? "Yes" : "No"

var msg = {"payload": "current: "+current_time+" - sunset: "+sunset, "before sunset? ": test};

return msg;

```

---

<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: [26 February 2024 11:06 UTC](https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917/2 "2024-02-26T11:06:05Z")

</div>

Do your conditional ternary before you format the time object moment creates.

```auto
var sunset_time = global.get('homeassistant').homeAssistant.states["sun.sun"].attributes.next_setting;

var current_time = moment().local();
var sunset = moment.utc(sunset_time).local();

var test = moment(current_time).isBefore(moment(sunset))? "Yes" : "No"
var msg = {"payload": "current: "+current_time.format('HH:mm')+" - sunset: "+sunset.format('HH:mm'), "before sunset? ": test};

return msg;

```

or something similar, untested.

---

<div class="post-metadata">

### Author: ![lee\_evo](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@lee\_evo](https://discourse.nodered.org/u/lee_evo)
#### Post date: [26 February 2024 11:16 UTC](https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917/3 "2024-02-26T11:16:06Z")

</div>

That did it !!!

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: [11 March 2024 11:16 UTC](https://discourse.nodered.org/t/using-moment-js-to-compare-times-in-function-node/85917/4 "2024-03-11T11:16:46Z")

</div>

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