Compare time now against time as a varible

Hi all im trying to compare 2 times and calculate the difference, one is from a trigger and the other is a pre-set time , I found how to check the times and get the time difference but i can work how to have a set time


Have you read some doco on js dates? JavaScript Date Set Methods should help you.

What format is the set time in?

I would like it to have HH:MM

Thanks I will have a look but JavaScript is new to me

What exactly are you asking? How to enter HH:MM in the dashboard? If not then what?

Hi Colin
I what to compare real time to a fixed time and calculate the differences , I cant work out how to create the fixed time eg 10:00:00

Do you mean (for example) "How long is it since 10 am?"
Or "How long is it since 10 am on 6th November 2022?"

How long since 10 am

You mean somthing like this?

let a = new Date().getHours() + 2 //the + 2 is for time zone correction
let b = 10
let c = a - b

msg.payload = c

return msg;

Thanks I was try to over complicate it

I was thinking of this approach. Probably also overcomplicated.

        "id": "a8a266724d4a4895",
        "type": "function",
        "z": "f3e02add17e4b873",
        "name": "function 18",
        "func": "var now = new Date();\nvar tenam= new Date()\ntenam.setHours(10,0,0,0)\nvar diff = now.getTime() - tenam.getTime()\n\nmsg.payload = {\"now\": now, \"then\": tenam, \"difference (seconds)\": diff/1000}\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 60,
        "wires": [

thanks for the suggestion

Just bear in mind that 'new Date()' is z time.

When you 'tenam.setHours(12,0,0,0)' you have to add time zone. I am at +2

{"now":"2022-11-11T14:13:08.881Z","then":"2022-11-11T10:00:00.000Z","difference (seconds)":15188.881}

Thanks I did spot tha,t Im in the UK so I have removed it.
Is there a way of getting to adjust for daylight saving

Yes with a if statement, check if it is between the certain two dates you change the value of 'tenam.setHours(12,0,0,0)'

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