# Need help in epoch time convert

**URL:** <https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690>\
**Category:** General\
**Created:** [7 October 2022 17:56 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690 "2022-10-07T17:56:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![boo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/boo/32/67804_2.png) [@boo](https://discourse.nodered.org/u/boo)\
**Post date:** [7 October 2022 17:56 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/1 "2022-10-07T17:56:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 18:02 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/2 "2022-10-07T18:02:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/boo/32/67804_2.png) [@boo](https://discourse.nodered.org/u/boo)\
**Post date:** [7 October 2022 18:06 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/3 "2022-10-07T18:06:39Z")

</div>

> [@E1cid](#):
>
> time stamp (now in milliseconds) from time now timestamp

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`

````auto
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;```
````

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 October 2022 18:14 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/4 "2022-10-07T18:14:36Z")

</div>

> [@boo](#):
>
> i tried below code(found from other post) and it works but i want the time in Indian stranded time (IST)

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 18:49 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/5 "2022-10-07T18:49:57Z")

</div>

```auto
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;

```

> **[Intl.DateTimeFormat() constructor - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat)**
>
> The Intl.DateTimeFormat() constructor creates
> Intl.DateTimeFormat objects that enable
> language-sensitive date and time formatting.

Output

```auto
Fri, Oct 7, 2022, 19:31

```

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

```auto
[{"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/](https://momentjs.com/docs/#/displaying/format/)

---

<div class="post-metadata">

**Author:** ![C.Sa](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@C.Sa](https://discourse.nodered.org/u/C.Sa)\
**Post date:** [7 October 2022 18:55 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/6 "2022-10-07T18:55:18Z")

</div>

> [@boo](#):
>
> but i want the time in Indian stranded time (IST) or simple readably time

Something like this?

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![boo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/boo/32/67804_2.png) [@boo](https://discourse.nodered.org/u/boo)\
**Post date:** [7 October 2022 19:37 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/7 "2022-10-07T19:37:53Z")

</div>

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

```auto
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;
```

---

<div class="post-metadata">

**Author:** ![boo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/boo/32/67804_2.png) [@boo](https://discourse.nodered.org/u/boo)\
**Post date:** [7 October 2022 20:05 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/8 "2022-10-07T20:05:28Z")

</div>

@E1cid what i see is time is out of sync  
its showing  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ed0644685e02d445f1a4b1c5cb43565b12c60b4.png)  
but the correct conversion for epoch time `1665170887` is

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/2671d15b020475b0571cf3f126a6635939515395.png)  
their is a 2 hour difference. any fix for this?  
`

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 20:15 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/9 "2022-10-07T20:15:26Z")

</div>

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

```auto
var time1 = (new Date(lastseen)).toLocaleString("IST",{
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    hour: 'numeric',
    minute: '2-digit',
    timeZone: "Asia/Kolkata"
});

```

```auto
[{"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

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

```

---

<div class="post-metadata">

**Author:** ![boo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/boo/32/67804_2.png) [@boo](https://discourse.nodered.org/u/boo)\
**Post date:** [7 October 2022 20:30 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/11 "2022-10-07T20:30:25Z")

</div>

thanks it works

---

<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:** [21 October 2022 20:30 UTC](https://discourse.nodered.org/t/need-help-in-epoch-time-convert/68690/12 "2022-10-21T20:30:36Z")

</div>

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