# Time and Date in the Node Ned

**URL:** https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461
**Category:** General
**Created:** [25 April 2020 07:38 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461 "2020-04-25T07:38:04Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [25 April 2020 07:38 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/1 "2020-04-25T07:38:05Z")

</div>

Hi,  
How can we get only the time from the timestamp (not whole date)?  
I have used this code in the function node which gives me the whole date ...

```auto
msg.payload={timestamp :new Date().toString ()}
return msg;

```

output is 🙂  
 ![time](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3ad2ba62bd40a413268d296e11c4f197e91b071.png)

while I want to get the time like 7:29 only ..........  
Please any idea.

---

<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: [25 April 2020 08:05 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/2 "2020-04-25T08:05:12Z")

</div>

The Date object provides lots of different functions to return different things, including just the time.

> **[Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)**
>
> JavaScript Date objects represent a single moment in time in a platform-independent format.

---

<div class="post-metadata">

### Author: ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)
#### Post date: [25 April 2020 08:11 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/3 "2020-04-25T08:11:00Z")

</div>

```auto
var a = new Date().toString ();
var parts = a.split(" ");

node.warn(parts);
node.warn(parts[4]);
node.warn(parts[4].split(":"));

var arr = parts[4].split(":");

msg.payload = arr[0] + ":" + arr[1];
return msg;

```

will output:

```auto
25-4-2020 10:08:39node: 24673420.b3a4dc
function : (warn)
array[10]
0: "Sat"
1: "Apr"
2: "25"
3: "2020"
4: "10:08:37"
5: "GMT+0200"
6: "(Central"
7: "European"
8: "Summer"
9: "Time)"

25-4-2020 10:08:39node: 24673420.b3a4dc
function : (warn)
"10:08:37"

25-4-2020 10:08:39node: 24673420.b3a4dc
function : (warn)
array[3]
0: "10"
1: "08"
2: "37"

25-4-2020 10:08:39node: 5a9bd7fb.aa9328
msg.payload : string[5]
"10:08"

```

---

<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: [25 April 2020 08:15 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/4 "2020-04-25T08:15:52Z")

</div>

No need to convert it to a string and make life difficult. You can get hours and minutes direct from the Date object by calling the appropriate functions.

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [26 April 2020 06:58 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/5 "2020-04-26T06:58:24Z")

</div>

Thanks for your replay ..

Yes, I have used it but still shown me full date 🙂

The code that used in the function node 🙂

```auto
function addZero(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function myFunction() {
  var d = new Date();
  var h = addZero(d.getHours());
  var m = addZero(d.getMinutes());
  var msg = h + ":" + m ;
}
return msg;

type or paste code here

```

In the output given me not only the time but also the date ...

---

<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: [26 April 2020 07:17 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/6 "2020-04-26T07:17:00Z")

</div>

You code defined a function called myFunction but it never gets called so the original msg passes straight through.

Which property of the message do you want the time to be stored in?  
If it's msg.payload, then you try use:

```auto
function addZero(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
msg.payload = h + ":" + m ;

return msg;

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [26 April 2020 07:28 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/7 "2020-04-26T07:28:27Z")

</div>

Yes, I want to in "msg .payload" ...... but have you notice that it does give the same time of the local computer ?? from where this time comes you think

---

<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: [26 April 2020 07:45 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/8 "2020-04-26T07:45:18Z")

</div>

It is the time of the device running Node-RED.

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [26 April 2020 07:49 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/9 "2020-04-26T07:49:33Z")

</div>

Oh ,that mean it shown me the time of the IBM cloud as its run on it ..

Thanks so much for all these information

---

<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: [26 April 2020 08:23 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/10 "2020-04-26T08:23:22Z")

</div>

This is a subflow I wrote which may be of use to you:

```auto
[{"id":"5a81f3be.561bdc","type":"subflow","name":"Time Stamp","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"682ac304.0590d4"}]}],"out":[{"x":640,"y":180,"wires":[{"id":"cf554e88.9fff18","port":0},{"id":"682ac304.0590d4","port":0}]},{"x":640,"y":230,"wires":[{"id":"682ac304.0590d4","port":0},{"id":"3db4df41.634848","port":0}]},{"x":640,"y":280,"wires":[{"id":"e4ca4227.3109c8","port":0},{"id":"682ac304.0590d4","port":0}]}],"env":[],"color":"#FF8888","outputLabels":["For use in log files","msg.time","For filename use"],"icon":"node-red/timer.svg"},{"id":"cf554e88.9fff18","type":"moment","z":"5a81f3be.561bdc","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_AU","output":"payload","outputType":"msg","outTz":"Australia/Sydney","x":390,"y":180,"wires":[["e4ca4227.3109c8","c810aa73.a5706"]]},{"id":"e4ca4227.3109c8","type":"string","z":"5a81f3be.561bdc","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":":"},{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":440,"y":280,"wires":[[]]},{"id":"c810aa73.a5706","type":"change","z":"5a81f3be.561bdc","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":230,"wires":[["3db4df41.634848"]]},{"id":"be4fd533.c9abb","type":"change","z":"5a81f3be.561bdc","name":"Save","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":140,"wires":[["c7ad4b3c.3a5368"]]},{"id":"3db4df41.634848","type":"change","z":"5a81f3be.561bdc","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":230,"wires":[[]]},{"id":"682ac304.0590d4","type":"switch","z":"5a81f3be.561bdc","name":"check topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"TIMESTAMP","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":100,"wires":[[],["be4fd533.c9abb"]]},{"id":"c7ad4b3c.3a5368","type":"change","z":"5a81f3be.561bdc","name":"TimeStamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":180,"wires":[["cf554e88.9fff18"]]},{"id":"2df84ea9.6dfc0a","type":"subflow:5a81f3be.561bdc","z":"e734e288.3b92f8","name":"","x":780,"y":1030,"wires":[[],[],[]]}]

```

---

<div class="post-metadata">

### Author: ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)
#### Post date: [26 April 2020 10:31 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/11 "2020-04-26T10:31:19Z")

</div>

@Trying_to_learn

Thanks ,That's very kind of you.

---

<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: [25 June 2020 10:31 UTC](https://discourse.nodered.org/t/time-and-date-in-the-node-ned/25461/12 "2020-06-25T10:31:22Z")

</div>

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