Date time = 7minutes %&*)^&

Ok, i go nuts...
I cannot get it working with all examples that i can find on google so i hope some one here knows the magic...

I have the date and time with the function below, but i want to add 7 minutes to it, and if it goes above 60 minutes it needs to go to the next hour and so on.

    var currentdate = new Date() + 60000; 
    var t2 = "Datum : " + new Date().today() + " - " + new Date().timeNow();

But i have the idea that timeNow makes it more difficult because i don't see that in all the examples

Thanks for your help :wink:

Its fun when you can't quite get things working.

Quick question: What is the + 60000 at the end of the first line?

(7 minutes isn't 60000 (anything))

What are you using to do this trick? Just maths or are you using other nodes?

// Get current time as millisecs since epoch
var now =;

// Add 7 minutes (in millisecs)
// (Edited because I got my maths wrong)
var then = now + 7*60*1000;

// Get Date object for 7 mins from now
var thenDate = new Date(then);
1 Like

This is a flow I just made (nothing against @knolleary) and though it uses the moment node, is ok.

[{"id":"7f497037.b6a308","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["715d141b.5acfd4","24d55edb.5f1792","5f87a73b.0cee58"]]},{"id":"5f87a73b.0cee58","type":"function","z":"b9924a74.4d98f8","name":"","func":"//  7 Minutes:  = 420 seconds\n//              = 420,000 milliseconds\n\nlet x = msg.payload;\nlet y = x + 420000;\nmsg.payload = y;\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":380,"wires":[["2d62af02.39668"]]},{"id":"24d55edb.5f1792","type":"moment","z":"b9924a74.4d98f8","name":"","topic":"","input":"","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"","output":"","outputType":"msg","outTz":"","x":490,"y":340,"wires":[["7a8932e6.53e174"]]},{"id":"2d62af02.39668","type":"moment","z":"b9924a74.4d98f8","name":"","topic":"","input":"","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"","output":"","outputType":"msg","outTz":"","x":490,"y":380,"wires":[["d4b5e8b9.1aeb58"]]},{"id":"7a8932e6.53e174","type":"debug","z":"b9924a74.4d98f8","name":"NOW","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":340,"wires":[]},{"id":"d4b5e8b9.1aeb58","type":"debug","z":"b9924a74.4d98f8","name":"Future","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":380,"wires":[]}]

Nick, sorry, but I am missing how that works.

I see now no problems and I can see how you get the then as it is pretty much what I did in my function node.

But the last line.....

I don't get it. Would you mind explaining how it works?

When you want a new Date object, you do:

var d = new Date()

This gives you a Date object that represents the time at the moment you called it.

However the Date constructor function can also be passed an argument. If that argument is a number, it is treated as a value of milliseconds since epoch (Jan 1st, 1970 for historic reasons) and the resulting Date object will represent that point in time instead of now.

Yeah, ok.... But.... Sorry.

If you load my flow, it works. No offence.

I saw yours and am not sure what the end user variable is.

Is it thenDate?

I tried that in my flow and it doesn't add the 7 minutes.

This is a screen show of the flow (first)
I'm saving electrons with the dark theme. :wink:

and this is the code.....

[{"id":"5f87a73b.0cee58","type":"function","z":"b9924a74.4d98f8","name":"","func":"//  7 Minutes:  = 420 seconds\n//              = 420,000 milliseconds\n\nlet x = msg.payload;\nlet y = x + 420000;\nmsg.payload = y;\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":380,"wires":[["2d62af02.39668"]]},{"id":"7f497037.b6a308","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["715d141b.5acfd4","24d55edb.5f1792","5f87a73b.0cee58","4b3cf3a3.11aae4"]]},{"id":"2d62af02.39668","type":"moment","z":"b9924a74.4d98f8","name":"","topic":"","input":"","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"","output":"","outputType":"msg","outTz":"","x":490,"y":380,"wires":[["d4b5e8b9.1aeb58"]]},{"id":"24d55edb.5f1792","type":"moment","z":"b9924a74.4d98f8","name":"","topic":"","input":"","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"","output":"","outputType":"msg","outTz":"","x":490,"y":340,"wires":[["7a8932e6.53e174"]]},{"id":"4b3cf3a3.11aae4","type":"function","z":"b9924a74.4d98f8","name":"","func":"// Get current time as millisecs since epoch\nvar now =;\n\n// Add 7 minutes (in millisecs)\nvar then = now + 7*60*60*1000;\n\n// Get Date object for 7 mins from now\nvar thenDate = new Date(then);\n\nmsg.payload = then;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":450,"wires":[["620ce857.0e48c"]]},{"id":"d4b5e8b9.1aeb58","type":"debug","z":"b9924a74.4d98f8","name":"Future","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":380,"wires":[]},{"id":"7a8932e6.53e174","type":"debug","z":"b9924a74.4d98f8","name":"NOW","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":340,"wires":[]},{"id":"620ce857.0e48c","type":"moment","z":"b9924a74.4d98f8","name":"","topic":"","input":"","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"","output":"","outputType":"msg","outTz":"","x":490,"y":450,"wires":[["ad2ee59.4548118"]]},{"id":"ad2ee59.4548118","type":"debug","z":"b9924a74.4d98f8","name":"Future 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":700,"y":450,"wires":[]}]

It isn't I want to hi-jack the thread, but I am not getting why it isn't adding the 7 minutes when I put in your but of code rather than mine.

The outputs are "now"
the second debug is my code
the third debug is with your code.

The method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

So if you add 60*1000 you add 1 minute.

If you add 7*60*1000* you add 7 mins.

The rest is as nick said

Edit corrected math

I'm not in a position to import a flow to look at it.

The code I provided wasn't a full and complete Function node example. It just showed the principle of how to get a Date object that represents 7 mins from now.

I got the maths wrong.

60*1000 is 1 minute in millisecs

So 7 mins is 7*60*1000.

Have edited my original reply.

No worries.

I basically did what you did. I added it to the now timestamp. (From the inject node)
I added 420,000 (7 minutes) and put it through the moment node to make it human readable.

I see a + 7 minute addition to the now time - and it is/was good that it was 59 minutes when posting as it shows the carry over working - and I see the expected (correct) future time.

Using your code and using the then variable from your code, I send that into the moment node and get the time now, not in 7 minutes time.

No problems. I'm stupid. I get that. I was just not understanding how to use the last line in the code you posted and which is the one you ultimately use.

Yes, I know I have kind of contradicted myself in the names I use/d in the code.

Originally I thought it is/was: thenDate and used then in the code. But I'm guessing thenDate is formatted? and then is just the time stamp of the future time.

So long as @RogierQ gets it.

That's ok about the maths being wrong, but if you look at the screen shot, you can see the time isn't changed from the original input value.

Again, I probably also made a mistake in what I did in the function node, but for the sake of clarity this is what I have in the second function node:

Ah! Yeah. Ok. The hours went crazy. I wasn't looking that much at that part.

My apologies.
(It happens)

Thanks @knolleary!

I see this still hasn't been resolved (solved)

Thinking from another angle:

You want something to happen in 7 minutes time. (Nominal value)

There is an event to trigger this whole thing happening.
Why not just use a delay node?

Set it for 7 minutes delay.

The signal is received.
It is delayed 7 minutes and sends on the signal.
This can then be used to trigger the things you want to be done being done.

Just another way of looking at the question.

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