UNIX Timestamp parsing

Currently i have an issue by parsing a UNIX timestamp which has the following format and is from datatype string:
"7147610980292335270"

Example Message Payload:
{ "id":"e34ea3ef-3896-4aaa-85e9-83e5fcf6f5e0", "sysTime":"7147610980292335270", "valueBool":true }

Currently i tried to parse it with the nodes from the moments module. And there is also a possibility to use the BigInt Module ( BigInt - JavaScript | MDN (mozilla.org)) which is used inside a function. The problem is that i need to set always the settings.js file within the NodeRED deployment.

So basically the question is: Is there any easier way to convert the UNIX timestamp from the string shown above?

Hi.

The first thing that has me worried.

string.

It should be a number.

(you could put it through a function node and make it a number.)

let x = parseInt(msg.payload);
msg.payload = x;
return msg;

Yes thats true, i was just trying to give an overview about the input format.

So the first step would be the conversion from string to number, but then i got also stuck by converting the number to the timestamp.

who is giving you the timestamp as a string?

(Poor example)

This is how you get "now" as a number/timestamp

change node set to output a timestamp as needed.
(Import to see what I mean)

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

Yes i tried that one as well, but the timestamp won't be the same since we have some processing time as well.

Ok, so again: who (or what) is sending you this message?

It is an MQTT Broker, but i don't want to change that configuration.

I think there is an underlying problem that will cause you a lot of headache in the future.

But as a hack you could try this (in a function node):

let msg.sysTime = parseInt(msg.sysTime);
return msg;

THat is not a string representation of a unix timestamp, if it was it would be the year 20000 ish. You need t find what format the sysTime is in.

1 Like

Indeed, in 174 years. Thu Jun 30 2196 23:29:40 UTC

There is always an online tool for converting anything :slight_smile: Unix Time Stamp - Epoch Converter

date - Convert a Unix timestamp to time in JavaScript - Stack Overflow

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