# Compare hours of different days

**URL:** https://discourse.nodered.org/t/compare-hours-of-different-days/72659
**Category:** General
**Created:** [24 December 2022 13:07 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659 "2022-12-24T13:07:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)
#### Post date: [24 December 2022 13:07 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/1 "2022-12-24T13:07:31Z")

</div>

the following code in a function:

```auto
var time = new Date();
var hour = time.getHours();
var checkhour = new Date(1671950776).getHours();

node.warn("current hour: " + hour);
node.warn("checkhour: " + checkhour);

if (hour > 12){
    node.warn("past 12!");
}

if (hour >= checkhour){
    node.warn("later than checkhour!");
} else if (hour < checkhour){
    node.warn("earlier than checkhour");
} else node.warn("confused");

```

the output of this:  
"current hour: 14"  
"checkhour: 9"  
"past 12!"  
"later than checkhour!"

the code works, if there is no daybreak between the compared timestamps.  
I tried with parseInt() but this does not help.  
how can I achieve that the numbers are really compared as numbers?

thanks! 🙂

Best regards and merry x-mas! 🙂

---

<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: [24 December 2022 14:00 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/2 "2022-12-24T14:00:38Z")

</div>

I must be missing something because it seems that:

- It _is_ past 12 (14 \> 12)
- hour _is_ later than checkhour (14 \> 9)
- The values _are_ compared as numbers (14 \> 9 whereas "14" \< "9")
- There _is_ a daybreak between the timestamps ( 1671950776 is 1970-01-20T08:25:50.776Z )

Can you explain what is wrong with your output?

---

<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: [24 December 2022 15:18 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/3 "2022-12-24T15:18:36Z")

</div>

> [@Cyber](#):
>
> `var checkhour = new Date(1671950776).getHours();`

Perhaps you meant 1671950776000. Javascript timestamps are in milliseconds, not seconds.

---

<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: [24 December 2022 15:56 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/4 "2022-12-24T15:56:06Z")

</div>

If there is a day break then you can not use hours to compare, You could just compare time object or the millisecond Values.  
e.g.

```auto
const time = new Date();
let hour = time.getHours();
//time.setHours(hour - 24); //remove 24hours for testing over a daybreak
let check_millis = time.getTime();
let millis = 1671896084855;

node.warn(`current hour: ${hour}
millis:${millis}
check_millis: ${check_millis}`);

if (hour > 12){
    node.warn("past 12!");
}

if (check_millis >= millis){
    node.warn("later than!");
} else if (check_millis < millis){
    node.warn("earlier than");
} else node.warn("confused");

```

---

<div class="post-metadata">

### Author: ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)
#### Post date: [24 December 2022 19:00 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/5 "2022-12-24T19:00:43Z")

</div>

okay, help me with the following code block please.

I have an object for cheap charging hours for an battery.:

```auto
cheapest5hours	
array[5]
0: array[2]
0: 8.564
1: "2022-12-25T05:00:00.000Z"
1: array[2]
0: 8.565000000000001
1: "2022-12-25T04:00:00.000Z"
2: array[2]
0: 8.57
1: "2022-12-25T03:00:00.000Z"
3: array[2]
0: 9.084
1: "2022-12-25T02:00:00.000Z"
4: array[2]
0: 9.27
1: "2022-12-25T06:00:00.000Z"

```

the object is generated once a day by a node in my flow and stored in flow.cheapest5hours  
the data period (where the cheapest 5 hours are available) are from 18:00 (6pm) until 9:00 (am) the next day.

now, in my minutely running flow I check hour and calculate how long my battery needs to charge today.  
now, my issue is: I know e.g. my battery needs to charge 5 hours. the cheapest hours are 5,4,3,2,6  
they are taken out from the flow.cheapest5hours in the same order to charge only in the not-so-cheap-hours the calculated difference.

but as the list is only as long as the needed calculated charged hours, already past hours need to be deleted out of the list. and in exactly this I am failing.

there is commented code where I tried to achieve this.

```auto
var chargehours = [];
if (batteryneedchargehours > 4.99) batteryneedchargehours = 4.99; // only cheapest 5h available
var chghourtmp = 0;
for (var i = 0; i < 5; i++) {
    chghourtmp = new Date(cheapest5hours[i][1]).getHours();
    chargehours.push(chghourtmp);
    //if (hour >= chghourtmp) chargehours.push(chghourtmp);
    if (chargehours.length >= batteryneedchargehours) break;
    // node.warn("current hour: " + hour + " chghourtmp: " + chghourtmp)
    // if ((hour > 17 && chghourtmp >= hour) || (hour < 10 && chghourtmp <= hour)) {
    // chargehours.push(chghourtmp);
    // } else node.warn("chargehour " + chghourtmp + " already in the past.");
}

```

my idea was: if var hour (which is current time.getHours()😉 is e.g. 22 (10pm) an entry for 21 is not put put in the list, it would be skipped.  
or, if the daybreak was, if hour = 4 (4am) an entry for 3 am is not put in the list.

the code part which switches on / off the charging (works):

```auto
if (chargehours.includes(hour)) {
....
}

```

---

<div class="post-metadata">

### Author: ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)
#### Post date: [25 December 2022 14:04 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/6 "2022-12-25T14:04:46Z")

</div>

ok, I guess I found a much more simple solution.  
on populating the list for the chargehours, I just compare the timestamps of:  
current  
and the one which should be filled in the list.  
if it is already in the past, then there is no need to put it in the list. 🥹

---

<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: [23 February 2023 14:05 UTC](https://discourse.nodered.org/t/compare-hours-of-different-days/72659/7 "2023-02-23T14:05:42Z")

</div>

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