# UNIX Timestamp parsing

**URL:** https://discourse.nodered.org/t/unix-timestamp-parsing/68224
**Category:** General
**Tags:** function-node
**Created:** [26 September 2022 09:10 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224 "2022-09-26T09:10:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![jakobgabriel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jakobgabriel/32/68166_2.png) [@jakobgabriel](https://discourse.nodered.org/u/jakobgabriel)
#### Post date: [26 September 2022 09:10 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/1 "2022-09-26T09:10:19Z")

</div>

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)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)) 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?

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 September 2022 09:16 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/2 "2022-09-26T09:16:16Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![jakobgabriel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jakobgabriel/32/68166_2.png) [@jakobgabriel](https://discourse.nodered.org/u/jakobgabriel)
#### Post date: [26 September 2022 09:18 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/3 "2022-09-26T09:18:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 September 2022 09:19 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/4 "2022-09-26T09:19:27Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![jakobgabriel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jakobgabriel/32/68166_2.png) [@jakobgabriel](https://discourse.nodered.org/u/jakobgabriel)
#### Post date: [26 September 2022 09:51 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/5 "2022-09-26T09:51:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 September 2022 09:52 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/6 "2022-09-26T09:52:32Z")

</div>

Ok, so again: _who_ (or _what_) is sending you this message?

---

<div class="post-metadata">

### Author: ![jakobgabriel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jakobgabriel/32/68166_2.png) [@jakobgabriel](https://discourse.nodered.org/u/jakobgabriel)
#### Post date: [26 September 2022 09:54 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/7 "2022-09-26T09:54:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 September 2022 09:56 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/8 "2022-09-26T09:56:44Z")

</div>

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

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

```

---

<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: [26 September 2022 10:00 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/9 "2022-09-26T10:00:38Z")

</div>

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.

---

<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: [26 September 2022 18:42 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/10 "2022-09-26T18:42:45Z")

</div>

> [@jakobgabriel](#):
>
> 7147610980292335270

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

There is always an online tool for converting anything 🙂 [Unix Time Stamp - Epoch Converter](https://www.unixtimestamp.com/)

[date - Convert a Unix timestamp to time in JavaScript - Stack Overflow](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript)

---

<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: [25 November 2022 18:43 UTC](https://discourse.nodered.org/t/unix-timestamp-parsing/68224/11 "2022-11-25T18:43:18Z")

</div>

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