# Utc number to time

**URL:** <https://discourse.nodered.org/t/utc-number-to-time/47552>\
**Category:** General\
**Created:** [23 June 2021 21:34 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552 "2021-06-23T21:34:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [23 June 2021 21:34 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/1 "2021-06-23T21:34:13Z")

</div>

hello

i call up a page and i got a objekt back

payload.passes[0].startUTC: 1624496900

in the node red debug it is possible to click a cuple of time to get  
24.6.2021, 03:08:20 [UTC+2]

but just to send this to telgram i still have the number how to change it to a time +2h for central Europe?

have a nice day  
vinc

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [23 June 2021 21:46 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/2 "2021-06-23T21:46:36Z")

</div>

let targetTime = msg.payload.passes[0].startUTC;  
let offset = 2;  
let tzDifference = offset \* 60 + targetTime.getTimezoneOffset();  
let time = new Date(targetTime.getTime() + tzDifference \* 60 \* 1000);

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [23 June 2021 21:59 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/3 "2021-06-23T21:59:02Z")

</div>

@ScheepersJohan  
thanks for the fast feedback

the function node get the error

function : (error)  
"TypeError: targetTime.getTimezoneOffset is not a function"

but will digg for it later

---

<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:** [23 June 2021 22:09 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/4 "2021-06-23T22:09:20Z")

</div>

Here is a little subflow I wrote to give you a _time stamp_.  
There are 3 outputs.  
1 - for use in log files so you can read it.  
2 - the time is in `msg.time` and can be used _downstream_ by other nodes.  
3 - used if you want to create file names with the date.

Just an offering.

```auto
[{"id":"5a81f3be.561bdc","type":"subflow","name":"Time Stamp","info":" **3 outputs. 1 - msg.payload holds the time. 2 - msg.time holds the time in a way to be used for reading time in a log file. 3 - outputs nsg.time in a format usable for file names**","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":"537c4cf1.68c114","type":"subflow:5a81f3be.561bdc","z":"e43e3902.57c4e8","name":"","x":770,"y":760,"wires":[["352be3cd.915574"],[],[]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 June 2021 22:17 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/5 "2021-06-23T22:17:18Z")

</div>

This can be done in the change node with a JSONata expression and $moment()

```auto
[{"id":"4749bb34.e9d6f4","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1624496900","payloadType":"num","x":240,"y":1500,"wires":[["ca7013e5.c256"]]},{"id":"ca7013e5.c256","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload*1000).tz(\"CET\").format(\"DD.MM.YYYY, HH:mm:ss Z\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1500,"wires":[["ef68f5ed.f012a"]]},{"id":"ef68f5ed.f012a","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1540,"wires":[]}]

```

```auto
$moment(payload*1000).tz("CET").format("DD.MM.YYYY, HH:mm:ss Z")

```

\*1000 as unix time is in seconds and JSONata uses milliseconds like Javascript

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [25 June 2021 19:08 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/6 "2021-06-25T19:08:50Z")

</div>

@E1cid  
works fine your 3 node  
i added to my function node an defined as a variable,  
like  
var (or let) test2 = $moment(msg.payload.passes[0].maxUTC\*1000).tz("CET").format("DD.MM.YYYY, HH:mm:ss Z");

which did not work ;-(

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 June 2021 19:25 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/7 "2021-06-25T19:25:28Z")

</div>

> [@E1cid](#):
>
> in the change node with a JSONata expression and $moment()

It is JSONata not Javascript. It will not work in a function node.

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [25 June 2021 19:55 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/8 "2021-06-25T19:55:27Z")

</div>

so i will have to split and join  
merci @E1cid

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [25 June 2021 19:59 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/9 "2021-06-25T19:59:07Z")

</div>

this would work too  
let start = new Date(msg.payload.passes[0].startUTC\*1000);

but have to find out about formatin it

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [25 June 2021 21:06 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/10 "2021-06-25T21:06:26Z")

</div>

```auto
function aLZ(n){
  if(n <= 9){
    return "0" + n;
  }
  return n
}

let startUTC = new Date(msg.payload.passes[0].startUTC*1000);
let Year = startUTC.getFullYear();
let Month = aLZ(startUTC.getMonth());
let Date2 = aLZ(startUTC.getDate());
let hours = aLZ(startUTC.getHours());
let minutes = aLZ(startUTC.getMinutes());
let seconds = aLZ(startUTC.getSeconds());

+"\r\n" +Date2 +"." +Month +"." +Year +"|" +hours +":"+minutes +":" +seconds

```

the aLZ is appendLeadingZeroes  
the line starting with +"\r\n" is insyde the message to send

found it there [How To Format Date In JavaScript | CodeHandbook](https://codehandbook.org/javascript-date-format/)

---

<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:** [26 June 2021 07:32 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/11 "2021-06-26T07:32:21Z")

</div>

> [@vikozo](#):
>
> `let Month = aLZ(startUTC.getMonth());`

You don't need your `aLZ()` function, you can use  
`let Month = startUTC.getMonth().toString().padStart(2,"0")`  
In fact for month I would have thought you wanted  
`let Month = (startUTC.getMonth() + 1).toString().padStart(2,"0")`

It would also be marginally better to use `const month = ` rather than `let` for all the variables which tells the interpreter that they will not change and may result in slightly more efficient code.

---

<div class="post-metadata">

**Author:** ![vikozo](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vikozo](https://discourse.nodered.org/u/vikozo)\
**Post date:** [26 June 2021 18:39 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/12 "2021-06-26T18:39:08Z")

</div>

Merci @Colin  
fun to see how many ways at the end are there.  
But what is the difference and when to use it (var, let or const)?

---

<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:** [26 June 2021 20:12 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/13 "2021-06-26T20:12:34Z")

</div>

> [@vikozo](#):
>
> what is the difference and when to use it (var, let or const)?

Google found this explanation for me [Var, Let, and Const – What's the Difference?](https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/)

---

<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:** [10 July 2021 20:13 UTC](https://discourse.nodered.org/t/utc-number-to-time/47552/14 "2021-07-10T20:13:27Z")

</div>

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