Timeticks convert to date

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.

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

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.

like this:

Dosent work for me

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

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.

1 Like

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) 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

1 Like

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

snmp looks like these:

flow looks like these:

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

1 Like

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

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.

1 Like

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.

1 Like

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

My function Code:

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

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:

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

Thanks for the help @Colin and @Buckskin

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)

  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

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

)

Also adjust locale string & timeZone to suit

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