# RFC3339 to Unix Timestamp

**URL:** <https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444>\
**Category:** General\
**Created:** [28 March 2022 10:47 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444 "2022-03-28T10:47:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [28 March 2022 10:47 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/1 "2022-03-28T10:47:40Z")

</div>

Hi all,  
Trying to convert timestamp for my database.  
My sensor outputs timestamp in this format `2022-03-22T06:17:28:410209Z` which I believe is RFC3339 according to influxdb

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/5693bd241f314dd1e91d0e3acc16ec47dc4c1e30.png)

But to my knowledge influxdb node only accepts timestamp in unix time and when trying to upload with that timestamp, I get `unable to parse 'topic measurement="value" 2022-03-22T06:17:28:410209Z': bad timestamp`

**How can I convert this from RFC3339 to Unix Timestamp?**

This is important as my sensors are wireless and they tend to drop connection but will still queue the outputs accordingly with timestamps included and send all when connected again

Here is the flow im playing around with to debug it

```auto
[{"id":"215c70b.6c8cf9","type":"change","z":"2c8ee17f.1fe80e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t {\t \"measurement\": msg.topic,\t \"fields\": msg.payload,\t \"timestamp\": msg.timestamp\t }\t] ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":300,"wires":[["e4439b3a42847739","d6929422ed15285f"]],"info":"To change the "},{"id":"88dd3939f719bca0","type":"inject","z":"2c8ee17f.1fe80e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"timestamp","v":"2022-03-22T06:17:28:410209Z","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic","payload":"{\"measurement\":69}","payloadType":"json","x":490,"y":300,"wires":[["215c70b.6c8cf9"]]},{"id":"e4439b3a42847739","type":"debug","z":"2c8ee17f.1fe80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1100,"y":300,"wires":[]},{"id":"d6929422ed15285f","type":"influxdb batch","z":"2c8ee17f.1fe80e","influxdb":"d32d1a0b4684e895","precision":"","retentionPolicy":"","name":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"Initial_Organization","bucket":"test","x":1100,"y":360,"wires":[]},{"id":"d32d1a0b4684e895","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"out","usetls":false,"tls":"","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":true}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 11:12 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/2 "2022-03-28T11:12:48Z")

</div>

What do you see in the debug node?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 11:14 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/3 "2022-03-28T11:14:58Z")

</div>

Also, are you trying to add a sample with the current timestamp, or with a timestamp that needs to be provided with the sample?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [28 March 2022 11:18 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/4 "2022-03-28T11:18:33Z")

</div>

> [@subwayfootlong](#):
>
> My sensor outputs timestamp in this format `2022-03-22T06:17:28:410209Z` which I believe is RFC3339 according to influxdb

That doesn't look like a RFC3339 Timestamp! There should be no colon after the 28,

```auto
2022-03-22T06:17:28.410209Z

```

would be correct.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 March 2022 11:55 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/5 "2022-03-28T11:55:51Z")

</div>

Assuming it's a point not a colon, something like this should provide the timestamp:

```auto
const input = "2022-03-22T06:17:28.410209Z";
const unixtime = new Date(input).valueOf();

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 12:39 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/6 "2022-03-28T12:39:51Z")

</div>

I don't think it wants unix time in fact. The normal way to do this would be to use  
`const time = new Date(input).getTime()`  
and then set the resolution in the influx node to milliseconds.

---

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [28 March 2022 13:58 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/7 "2022-03-28T13:58:05Z")

</div>

Hi currently im not in the office with the sensors, but the msg.payload contains this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed82802a3f45885a1ea27bddfff60b1a2f58d073.jpeg)

And yes, the current timestamp property contains a sample of what the sensor provides

@ghayne  
Ahh thanks for catching that. But the sensor still outputs a : . I tried inputting with the . and it returns the same error.

@jbudd  
Ah yes this works assuming that the point is not a colon. Now just need to find a way to change it to a point

```auto
[{"id":"9fd92f514c43c23f","type":"change","z":"2c8ee17f.1fe80e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t {\t \"measurement\": msg.topic,\t \"fields\": msg.payload,\t \"timestamp\": msg.timestamp\t }\t] ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":560,"wires":[["40226691aec02bb3","b2be9aa117b910cf"]],"info":"To change the "},{"id":"f229dd90d606ea5e","type":"inject","z":"2c8ee17f.1fe80e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"timestamp","v":"2022-03-22T06:17:28.410209Z","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic","payload":"{\"measurement\":69}","payloadType":"json","x":330,"y":560,"wires":[["0bd984c323e210cc"]]},{"id":"40226691aec02bb3","type":"debug","z":"2c8ee17f.1fe80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1120,"y":560,"wires":[]},{"id":"b2be9aa117b910cf","type":"influxdb batch","z":"2c8ee17f.1fe80e","influxdb":"d32d1a0b4684e895","precision":"","retentionPolicy":"","name":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"Initial_Organization","bucket":"test","x":1120,"y":620,"wires":[]},{"id":"0bd984c323e210cc","type":"function","z":"2c8ee17f.1fe80e","name":"","func":"const input = msg.timestamp;\nconst unixtime = new Date(input).valueOf();\nmsg.timestamp=unixtime;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":560,"wires":[["9fd92f514c43c23f"]]},{"id":"d32d1a0b4684e895","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"out","usetls":false,"tls":"","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":true}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 14:17 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/9 "2022-03-28T14:17:34Z")

</div>

What are these sensors that are sending invalid timestamps?

---

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [28 March 2022 14:25 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/10 "2022-03-28T14:25:14Z")

</div>

@Colin  
The sensors are from a local startup so I cant really blame them. I wont say their name but you can sort of see the company name from the debug node in the image 😉

Anyways I managed to change the colon to a period with help from this [stackoverflow question](https://stackoverflow.com/questions/44568657/how-to-replace-the-second-occurrence-of-a-string-in-javascript)

```auto
[{"id":"1fbd3937e7927ca0","type":"change","z":"2c8ee17f.1fe80e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t {\t \"measurement\": msg.topic,\t \"fields\": msg.payload,\t \"timestamp\": msg.timestamp\t }\t] ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":520,"wires":[["c66c890a2dedeaba","3645eb887c954b5b"]],"info":"To change the "},{"id":"e3cd5154ee418319","type":"inject","z":"2c8ee17f.1fe80e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"timestamp","v":"2022-03-22T06:17:28:410209Z","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic","payload":"{\"measurement\":69}","payloadType":"json","x":250,"y":520,"wires":[["48b7c4a188773e04"]]},{"id":"c66c890a2dedeaba","type":"debug","z":"2c8ee17f.1fe80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1040,"y":520,"wires":[]},{"id":"3645eb887c954b5b","type":"influxdb batch","z":"2c8ee17f.1fe80e","influxdb":"d32d1a0b4684e895","precision":"","retentionPolicy":"","name":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"Initial_Organization","bucket":"test","x":1040,"y":580,"wires":[]},{"id":"48b7c4a188773e04","type":"function","z":"2c8ee17f.1fe80e","name":"","func":"const input = msg.timestamp;\nlet t = 0\nconst result = input.replace(/:/g, match => ++t === 3 ? '.' : match)\nconst unixtime = new Date(result).valueOf();\nmsg.timestamp=unixtime;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":520,"wires":[["1fbd3937e7927ca0","ccf3212992f4bcdb"]]},{"id":"ccf3212992f4bcdb","type":"debug","z":"2c8ee17f.1fe80e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"timestamp","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":460,"wires":[]},{"id":"d32d1a0b4684e895","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"out","usetls":false,"tls":"","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":true}]

```

Only thing left to do is figure out why the sensors give me values of a few days back.... the image I took was from today.  
Thanks all for all your help!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 14:30 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/11 "2022-03-28T14:30:55Z")

</div>

> [@subwayfootlong](#):
>
> figure out why the sensors give me values of a few days back

Likely the time in the sensor is not correct.

You did not answer the question as to whether you actually need to use the time from the sensor. If the time should be the current time then use the Influx Out node (not the batch node) and it will use the current time.

I suggest feeding back to the startup that they have not formatted the date correctly, so they can fix it.

---

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [28 March 2022 14:33 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/12 "2022-03-28T14:33:28Z")

</div>

Yeah sorry for not being clear.

I need to use the time from the sensor as the sensors are placed a distance away from the gateway with metal machines in between.

So when the sensors actually connect, it will send all the unsent values at once and this causes influxdb to take it in at the same time as it uses the servers current time.

Will feedback! Thank you @Colin

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 15:30 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/13 "2022-03-28T15:30:33Z")

</div>

Understood, then yes, the influx batch node is the right one to use.

---

<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:** [27 May 2022 15:30 UTC](https://discourse.nodered.org/t/rfc3339-to-unix-timestamp/60444/14 "2022-05-27T15:30:35Z")

</div>

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