Need help in epoch time convert

HI, from data of previous node(sensor) i get time as epoch format i guess
"added_on":1665163868
what i want is: compare this time with the current now time and if the difference between them is less than 5 mins then I send some value to next node.
can someone help?
hard part is i dont know how to convert current time to epoch or epoch time received to other format.
also i want to make the received epoch time to readably time format. and send as sting

5 minutes is 300 seconds, times 1000 is 300000 miliseconds.
Your add_on is in seconds, so you need to multiply it by 1000.
Now subtract add_on timestamp (now in milliseconds) from time now timestamp, if the result is less then 300000 then its less than five minutes.

can you help this part?

also to convert epoch time to readably time:
i tried below code(found from other post) and it works but i want the time in Indian stranded time (IST) or simple readably time like Fri, 07 Oct 2022 19:31

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

return msg;```

As we are now using 21st C JavaScript, you can use the INTL library which is built in to node.js. Or better still, keep everything in UTC until you need to show it to a user or get their input. then you will avoid all of the horrid edge-cases and mistakes made when trying to work across time zones and DST changes.

let string_lastseen = "1665163868";
//convert from seconds to milliseconds and add 2 hours
let lastseen = Number(string_lastseen) * 1000 + 7200000;
msg.payload = (new Date(lastseen)).toLocaleString("IST",{
  weekday: 'short',
  year: 'numeric',
  month: 'short',
  day: 'numeric',
  hour: '2-digit',
  hourCycle: 'h24',
  minute: '2-digit',
  timeZone: 'UTC'
});

return msg;

Output

Fri, Oct 7, 2022, 19:31

but i prefer moment() in a change node using JSONata
here is an example of both.

[{"id":"b7c83f86c603d796","type":"inject","z":"452103ea51141731","name":"less","props":[{"p":"payload.add_on","v":"1665166260","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":430,"y":520,"wires":[["8970d6b2ad79662d","47be221ca89db078"]]},{"id":"8970d6b2ad79662d","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(($$.payload.add_on * 1000) + 7200000).tz(\"Asia/Kolkata\").format(\"ddd, DD MMM YYYY HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":560,"wires":[["06cbc13792c1ad0e"]]},{"id":"47be221ca89db078","type":"function","z":"452103ea51141731","name":"function 9","func":"let string_lastseen = msg.payload.add_on;\n//convert from seconds to milliseconds and add 2 hours\nlet lastseen = Number(string_lastseen) * 1000 + 7200000;\nmsg.payload = (new Date(lastseen)).toLocaleString(\"IST\",{\n  weekday: 'short',\n  day: '2-digit',\n  month: 'short',\n  year: 'numeric',\n  hour: '2-digit',\n  hourCycle: 'h24',\n  minute: '2-digit',\n  timeZone:\"Asia/Kolkata\"\n});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":500,"wires":[["06cbc13792c1ad0e"]]},{"id":"06cbc13792c1ad0e","type":"debug","z":"452103ea51141731","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":560,"wires":[]}]

https://momentjs.com/docs/#/displaying/format/

1 Like

Something like this?

var sensor = Number(msg.added_on)*1000;	  // to milliseconds
var dif = Date.now() - sensor;	  // Date.now() returns the number of ms, not a date object
if (dif <= 5*60*1000) {   // if less than 5min
	var addtwo = new Date(sensor+2*60*60*1000);   // sensor time +2h like in the previous example
	msg.payload = addtwo.toLocaleString();   // to string, assuming you're in india or similar
} else { 
	msg.payload = "an alternative?";
}
return msg;
1 Like

Edit: it works i made a mistake

thanks
got it Sat, Oct 8, 02:58 AM
is it possible to remove , soo it looks like this?
Sat Oct 8 02:58 AM
tried this but no luck

var time1 = (new Date(lastseen)).toLocaleString("IST",{
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    hourCycle: 'h12',
    minute: '2-digit',
    timeZone: 'IST'
});
var time2 = time1.replace(/,/g, '')
msg.time2 = time2;

@E1cid what i see is time is out of sync
its showing
image
but the correct conversion for epoch time 1665170887 is


their is a 2 hour difference. any fix for this?
`

The time is corrrect, remember you added 7200000 milliseconds to the timestamp.
As to format

var time1 = (new Date(lastseen)).toLocaleString("IST",{
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    hour: 'numeric',
    minute: '2-digit',
    timeZone: "Asia/Kolkata"
});
[{"id":"b7c83f86c603d796","type":"inject","z":"452103ea51141731","name":"less","props":[{"p":"payload.add_on","v":"1665170887","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":430,"y":520,"wires":[["8970d6b2ad79662d","47be221ca89db078"]]},{"id":"8970d6b2ad79662d","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(($$.payload.add_on * 1000) + 7200000).tz(\"Asia/Kolkata\").format(\"ddd D MMM h:mm A\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":560,"wires":[["06cbc13792c1ad0e"]]},{"id":"47be221ca89db078","type":"function","z":"452103ea51141731","name":"function 9","func":"let string_lastseen = msg.payload.add_on;\n//convert from seconds to milliseconds and add 2 hours\nlet lastseen = Number(string_lastseen) * 1000 + 7200000;\nvar time1 = (new Date(lastseen)).toLocaleString(\"IST\",{\n    weekday: 'short',\n    month: 'short',\n    day: 'numeric',\n    hour: 'numeric',\n    minute: '2-digit',\n    timeZone: \"Asia/Kolkata\"\n});\nvar time2 = time1.replace(/,/g, '')\nmsg.payload = time2;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":500,"wires":[["06cbc13792c1ad0e"]]},{"id":"06cbc13792c1ad0e","type":"debug","z":"452103ea51141731","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":560,"wires":[]}]

and without 2 hours (7200000 miliseconds) added

[{"id":"b7c83f86c603d796","type":"inject","z":"452103ea51141731","name":"less","props":[{"p":"payload.add_on","v":"1665170887","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":430,"y":520,"wires":[["8970d6b2ad79662d","47be221ca89db078"]]},{"id":"8970d6b2ad79662d","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(($$.payload.add_on * 1000) ).tz(\"Asia/Kolkata\").format(\"ddd D MMM h:mm A\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":560,"wires":[["06cbc13792c1ad0e"]]},{"id":"47be221ca89db078","type":"function","z":"452103ea51141731","name":"function 9","func":"let string_lastseen = msg.payload.add_on;\n//convert from seconds to milliseconds\nlet lastseen = Number(string_lastseen) * 1000;\nvar time1 = (new Date(lastseen)).toLocaleString(\"IST\",{\n    weekday: 'short',\n    month: 'short',\n    day: 'numeric',\n    hour: 'numeric',\n    minute: '2-digit',\n    timeZone: \"Asia/Kolkata\"\n});\nvar time2 = time1.replace(/,/g, '')\nmsg.payload = time2;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":500,"wires":[["06cbc13792c1ad0e"]]},{"id":"06cbc13792c1ad0e","type":"debug","z":"452103ea51141731","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":560,"wires":[]}]
1 Like

thanks it works

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