# Timeticks convert to date

**URL:** https://discourse.nodered.org/t/timeticks-convert-to-date/70904
**Category:** Dashboard
**Tags:** node-red-dashboard, function-node
**Created:** [20 November 2022 08:07 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904 "2022-11-20T08:07:31Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 08:07 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/1 "2022-11-20T08:07:31Z")

</div>

I am using snmp at the moment. Now I want to output the runtime of the server via snmp per oid. This succeeds me also. But the format is in "timeticks" as output.  
How can I convert this to dd:hh:mm:ss?  
I have tried it with a function, this does not work so well because it displays the hours in total hours.

```auto
var timeticks = msg.payload[0].value;
var stunden = Math.floor(timeticks / 360000);
var tage = Math.floor(timeticks / 8640000);
var result = ("Tage: " + tage + " | Stunden: " + stunden);
msg.payload[0].value= result;
return msg;

```

thank you for your help

---

<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: [20 November 2022 09:15 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/2 "2022-11-20T09:15:39Z")

</div>

Are these in milliseconds? If so then it should be 3600000. However a better way is something like  
`const datetime = new Date(timeticks)`  
then you can format it how you like using the Date functions.

---

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 09:38 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/3 "2022-11-20T09:38:07Z")

</div>

> [@Colin](#):
>
> const datetime = new Date(timeticks)

like this:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1ba71920b3bd0e8da5d3c241ae87a3a1e6bec93.png)

Dosent work for me

---

<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: [20 November 2022 09:58 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/4 "2022-11-20T09:58:08Z")

</div>

Show us what is going into the function and what is coming out please. Is that debug node direct on the function output?

---

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 10:49 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/5 "2022-11-20T10:49:09Z")

</div>

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/8750dd8471914b7402c41abf84c7b190e36cda9c.png)

---

<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: [20 November 2022 11:48 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/6 "2022-11-20T11:48:00Z")

</div>

Please give us some clues about what that is showing. They appear to be all coming from the same debug node. We need to see the data going in to the function and the data coming out.

---

<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: [20 November 2022 11:50 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/7 "2022-11-20T11:50:07Z")

</div>

Also please just show us that data not the whole screen. It is almost impossible to see what you are posting. If fact if you hover over the value in the debug pane then some buttons will popup. Click on the Copy button and paste that here, for the data going in and the data coming out. See this canned text for how to do that:

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 12:20 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/8 "2022-11-20T12:20:17Z")

</div>

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d5fa50834a108a4940882b3fd625485b80545a3.png)

Upper debog is direct from the snmp part  
and the second one from the fucntion part

snmp looks like these:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04258d734bf4c1ba94b8fde2b3ae8e4e6e5acc1f.png)

flow looks like these:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c77da93cd61216c4b857572665c6ad933cd0c06.png)

---

<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: [20 November 2022 12:34 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/9 "2022-11-20T12:34:33Z")

</div>

Sorry, I thought you were dealing with a timestamp, not an elapsed time. What units is the time in?

---

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 12:47 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/10 "2022-11-20T12:47:25Z")

</div>

As "TimeTricks" as output. But I want it in dd:hh:mm:ss. The conversion is my problem

---

<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: [20 November 2022 13:26 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/11 "2022-11-20T13:26:01Z")

</div>

The reason for my confusion initially was because the title of the thread said you wanted to convert to a date.

When I said what units I meant microseconds, milliseconds, seconds, or whatever.

---

<div class="post-metadata">

### Author: ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)
#### Post date: [20 November 2022 13:27 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/12 "2022-11-20T13:27:03Z")

</div>

So, this is not a timestanp but rather an uptime from when the snmp device had started (ticking), right?

Look into Moment.js ...it has a duration and diff function.  
Where: endTime = now() and startTime = endTime - your-snmp-ticks.  
Then: duration = moment.duration(endTime.diff(startTime))

..currently on the road, so can't test it myself, but this is what it should liik like.

---

<div class="post-metadata">

### Author: ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)
#### Post date: [20 November 2022 13:35 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/13 "2022-11-20T13:35:17Z")

</div>

I make a way, its realy ugly i think so, but it works.

My function Code:

```auto
var timeticks = msg.payload[0].value;
var timetickssec = timeticks / 100;
var timeTage = Math.floor(timetickssec / 86400);
var timeStunde = Math.floor((timetickssec - (timeTage * 86400))/3600);
var timeMinute = Math.floor((timetickssec - (timeTage * 86400) - (timeStunde * 3600))/60);
var timeSekunde = Math.floor((timetickssec - (timeTage * 86400) - (timeStunde * 3600) - (timeMinute * 60)));
msg.payload[0].value = (timeTage + ":" + timeStunde + ":" +timeMinute + ":" + timeSekunde);
return msg;

```

Output:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1baf44dd508d34d6feb7ea5416ece8d9d2f6a38b.png)

Are there a quick solution to make the :3: to a :03:  
So that they dosent have one one number at the hour part?

//Edit:  
This is the final solution for me:

```auto
var timeticks = msg.payload[0].value;
var timetickssec = timeticks / 100;
var timeTage = Math.floor(timetickssec / 86400);
var timeStunde = Math.floor((timetickssec - (timeTage * 86400)) / 3600);
var timeMinute = Math.floor((timetickssec - (timeTage * 86400) - (timeStunde * 3600)) / 60);
var timeSekunde = Math.floor((timetickssec - (timeTage * 86400) - (timeStunde * 3600) - (timeMinute * 60)));
msg.payload[0].value = (String(timeTage).padStart(2, "0") + ":" + String(timeStunde).padStart(2, "0") + ":" + String(timeMinute).padStart(2, "0") + ":" + String(timeSekunde).padStart(2, "0"));
return msg;

```

Output:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e99ccfe3f8aab03cabb2106e7e280c9b0bcd442c.png)

Thanks for the help @Colin and @Buckskin

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [20 November 2022 20:28 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/14 "2022-11-20T20:28:07Z")

</div>

Assuming that the `87133392` shown in the panel above as a value is in Centi seconds (and given that you divide by 100 to get seconds)

```auto
  const timeticks = msg.payload[0].value    
  const milliseconds = timeticks * 10 // Or whatever it takes to convert timeticks to milliseconds

  const date = new Date(milliseconds)
  const days = Math.floor(milliseconds / 86400000)

  return `${days}T${date.toLocaleTimeString('de-DE', { timeZone: 'UTC' })}`

```

gives `1T02:02:13`. The return string can be configured however you wish. I use this format because this is the format used by Tasmota. (So to get the same format as your output above use

```auto
`${days}:${date.toLocaleTimeString('de-DE', { timeZone: 'UTC' })}`

```

)

Also adjust locale string & timeZone to suit

---

<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: [4 December 2022 20:28 UTC](https://discourse.nodered.org/t/timeticks-convert-to-date/70904/15 "2022-12-04T20:28:52Z")

</div>

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