# Date time = 7minutes %&\*)^&

**URL:** <https://discourse.nodered.org/t/date-time-7minutes/26341>\
**Category:** General\
**Created:** [8 May 2020 22:34 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341 "2020-05-08T22:34:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [8 May 2020 22:34 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/1 "2020-05-08T22:34:10Z")

</div>

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.

```auto
    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 😉

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 22:43 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/2 "2020-05-08T22:43:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 May 2020 22:51 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/3 "2020-05-08T22:51:48Z")

</div>

```auto
// Get current time as millisecs since epoch
var now = Date.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);

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 22:53 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/4 "2020-05-08T22:53:06Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 22:55 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/5 "2020-05-08T22:55:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 May 2020 22:59 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/6 "2020-05-08T22:59:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> I don't get it. Would you mind explaining how it works?

When you want a new Date object, you do:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 23:05 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/7 "2020-05-08T23:05:03Z")

</div>

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. 😉

 ![Screenshot from 2020-05-09 09-02-22](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b03e93913a8b4059dcd560693e85a4a1ec518e01.png)

and this is the code.....

```auto
[{"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 = Date.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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2020 23:08 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/8 "2020-05-08T23:08:25Z")

</div>

The **`Date.now()`** 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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 May 2020 23:09 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/9 "2020-05-08T23:09:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 May 2020 23:12 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/10 "2020-05-08T23:12:01Z")

</div>

I got the maths wrong.

`60*1000` is 1 minute in millisecs

So 7 mins is `7*60*1000`.

Have edited my original reply.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 23:15 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/11 "2020-05-08T23:15:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 May 2020 23:22 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/12 "2020-05-08T23:22:33Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [9 May 2020 06:48 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/13 "2020-05-09T06:48:33Z")

</div>

Thanks @knolleary!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 May 2020 23:47 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/14 "2020-05-20T23:47:46Z")

</div>

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.

---

<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:** [19 July 2020 23:47 UTC](https://discourse.nodered.org/t/date-time-7minutes/26341/15 "2020-07-19T23:47:59Z")

</div>

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