Converting epoch time

#1

Hello

I have the following code in a function node. I am trying to convert string_lastseen into a readable format (string_lastseen = 1554625504). But the date function keeps returning what looks like the current time/date for the AWS machine it is running on.

How do you represent the above mentioned string in a readable date format? Is there a function for this?

var string_lastseen  = Object.values(msg.payload.Items[msg.payload.ScannedCount -1].Time);

var lastseen = Number(string_lastseen)+72000000;
var lastseenpayload = Date(lastseen);


msg.payload = lastseenpayload;

return msg;


0 Likes

#2

take a look at node-red-contrib-moment

0 Likes

#3

So from: https://www.epochconverter.com/

the value 1554625504
in epoch SECONDS represents GMT: Sunday, April 7, 2019 8:25:04 AM

(I imagine that is what you are going for)

and perhaps you would like to add two hours (hence the 7200,000?)
to get that in your node-red function node (javascript) you can do:

var string_lastseen = "1554625504";
var lastseen = 7200000+Number(string_lastseen)*1000;//convert from seconds to milliseconds
and add 2 hours
msg.payload = (new Date(lastseen)).toUTCString();

return msg;

Is that sort of what you are going for?

Here is the snippet:
[
    {
        "id": "d0d30a38.451ae8",
        "type": "tab",
        "label": "Temp",
        "disabled": false,
        "info": ""
    },
    {
        "id": "943a111f.ec936",
        "type": "inject",
        "z": "d0d30a38.451ae8",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 100,
        "y": 160,
        "wires": [
            [
                "b3d73c8e.3c21"
            ]
        ]
    },
    {
        "id": "db9500c2.5a57b",
        "type": "debug",
        "z": "d0d30a38.451ae8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 490,
        "y": 160,
        "wires": []
    },
    {
        "id": "b3d73c8e.3c21",
        "type": "function",
        "z": "d0d30a38.451ae8",
        "name": "",
        "func": "var string_lastseen  = \"1554625504\";\nvar lastseen = 7200000+Number(string_lastseen)*1000;//convert from seconds to milliseconds and add 2 hours\nmsg.payload = (new Date(lastseen)).toUTCString();\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 290,
        "y": 160,
        "wires": [
            [
                "db9500c2.5a57b"
            ]
        ]
    }
]
0 Likes