Compare hours of different days

the following code in a function:

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! :slight_smile:

Best regards and merry x-mas! :slight_smile:

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?

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

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.

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");

okay, help me with the following code block please.

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

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.

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():wink: 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):

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

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. :face_holding_back_tears:

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