# Convert timestamp 2021-09-24T16:01:14.979695752Z

**URL:** <https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339>\
**Category:** General\
**Created:** [7 November 2021 14:55 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339 "2021-11-07T14:55:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [7 November 2021 14:55 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/1 "2021-11-07T14:55:07Z")

</div>

I am not sure if this question has been already answered but I have not found it. Eventually.....sorry for asking the same question.

I would like to convert the timestamp given in this format:  
2021-09-24T16:01:14.979695752Z  
to date and time with milliseconds.

I would appreciate if you can point me in the right direction.

Thank you

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 November 2021 15:11 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/2 "2021-11-07T15:11:19Z")

</div>

function node...

```auto
var dt = "2021-09-24T16:01:14.979695752Z"; //this could be something from the incoming msg
var d = new Date(dt)
msg.payload = d;
return msg;

```

proof...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/9819eecec77946579b8429bdcd4f89439dfcee30.png)

Ref...

> **[Date - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)**
>
> JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [7 November 2021 15:38 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/3 "2021-11-07T15:38:09Z")

</div>

Thanks. I am getting close but still returning

**Fri Sep 24 2021 18:01:09 GMT+0200 (South Africa Standard Time)**

and not a simple date time. Maybe a should try to format d

Thanks

---

<div class="post-metadata">

**Author:** ![PdeJ](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@PdeJ](https://discourse.nodered.org/u/PdeJ)\
**Post date:** [7 November 2021 15:55 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/4 "2021-11-07T15:55:53Z")

</div>

Have a look at [Jsonata date time functions](https://docs.jsonata.org/date-time-functions)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [7 November 2021 16:39 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/5 "2021-11-07T16:39:33Z")

</div>

Why not give us an example of the format that you are trying to achieve?  
How do you want it presented?

---

<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 November 2021 17:29 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/6 "2021-11-07T17:29:44Z")

</div>

If you want it in milliseconds then

```auto
var dt = "2021-09-24T16:01:14.979695752Z";
var d = new Date(dt).valueOf();
msg.payload = d;
return msg;

```

or JSONata in a change node

```auto
$toMillis($$.payload)

```

if payload contains "2021-09-24T16:01:14.979695752Z"

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [7 November 2021 18:01 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/7 "2021-11-07T18:01:00Z")

</div>

> [@E1cid](#):
>
> `"2021-09-24T16:01:14.979695752Z"`

From this:  
"2021-09-24T16:01:14.979695752Z"  
I am trying to get this:  
2021-09-24 16:01:14.979695752  
Maybe I should simply manipulate it as string removing the T and the Z. Any other attempt I tried, also the above hints, gives me always something diferent.

---

<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 November 2021 18:26 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/8 "2021-11-07T18:26:26Z")

</div>

Then try  
Javascript  
`msg.payload = msg.payload.split(/T|Z/).join(" ").trim();`

JSONata  
`$join($split($$.payload, /T|Z/, 2), " ")`

---

<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:** [7 November 2021 19:06 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/9 "2021-11-07T19:06:09Z")

</div>

Looking through stackexchange suggests this:

```auto
Date.prototype.datepart = function () { 
    return this.getFullYear() +"-" +
    String((this.getMonth()+1)).padStart(2, '0') + "-" + 
    String(this.getDate()).padStart(2, '0');
}

Date.prototype.timepart = function () {
     return String(this.getHours()).padStart(2, '0') + ":" + 
     String(this.getMinutes()).padStart(2, '0') + ":" + 
     String(this.getSeconds()).padStart(2, '0') + "." +
     String(this.getMilliseconds()).padStart(3, '0');
}

var newDate = new Date(msg.payload);
var datetime = newDate.datepart() + " " + newDate.timepart();
msg.payload = datetime;
return msg;

```

> [@FeliceM](#):
>
> I am trying to get this:  
> 2021-09-24 16:01:14.979695752

That isn't milliseconds!

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [7 November 2021 19:14 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/10 "2021-11-07T19:14:42Z")

</div>

Try this, it is milliseconds!  
(Finer granularity that milliseconds in node-RED is not really practical, because of delays in the way which the system runs node-RED)

![seconds](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c5e00eabacdc6fcaf646f193175967889e4a0259.jpeg)

```auto
[{"id":"b39ddfe2f43f3986","type":"inject","z":"b3b413d1.05b1b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2021-09-24T16:01:14.979695752Z","payloadType":"str","x":170,"y":1540,"wires":[["d3de07c8de8a5263"]]},{"id":"7918130156c85d93","type":"debug","z":"b3b413d1.05b1b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":500,"y":1540,"wires":[]},{"id":"d3de07c8de8a5263","type":"change","z":"b3b413d1.05b1b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).format('YYYY-MM-DD HH:mm:ss.SSS')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":1540,"wires":[["7918130156c85d93"]]}]

```

The input is "2021-09-24T16:01:14.979695752Z"  
The output is "2021-09-24 17:01:14.979"

Or if you want it to create the output based upon current time/date, try;

```auto
[{"id":"32ca630295eac4aa","type":"inject","z":"b3b413d1.05b1b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":165,"y":1625,"wires":[["980aa508d76b5348"]]},{"id":"ee4f62696d2a6f79","type":"debug","z":"b3b413d1.05b1b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":495,"y":1625,"wires":[]},{"id":"980aa508d76b5348","type":"change","z":"b3b413d1.05b1b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($now()).format('YYYY-MM-DD HH:mm:ss.SSS')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":325,"y":1625,"wires":[["ee4f62696d2a6f79"]]}]

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [7 November 2021 19:48 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/11 "2021-11-07T19:48:13Z")

</div>

Try just using the change node to replace the T with space and the Z with nothing  
Sorry about replying to wrong person

---

<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:** [7 November 2021 20:00 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/12 "2021-11-07T20:00:07Z")

</div>

I'll live! 😀

---

<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:** [7 November 2021 20:07 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/13 "2021-11-07T20:07:15Z")

</div>

Why does an input of ...16:01:14... give an output of ...05:01:14... ?  
Is that a timezone difference?

And @FeliceM, your timestamp is UTC ( ~= GMT = Zulu), do you want the output to be UTC or local time?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [7 November 2021 20:13 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/14 "2021-11-07T20:13:40Z")

</div>

@jbudd good spot!!  
I had a typo in the change node - `hh` returns the hour in 12hr format, whilst `HH` returns the hour in 24hr format (which we want).  
I've updated the flows 😉

And yes, when converting the string (flow #1) there is an hour timezone conversion (not sure why?? - above my paygrade!)  
When generating the time/date (flow #2) the result is correct for the users own timezone.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [8 November 2021 04:23 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/15 "2021-11-08T04:23:34Z")

</div>

This works great!  
Thank you all for your assistance.

---

<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:** [22 November 2021 04:24 UTC](https://discourse.nodered.org/t/convert-timestamp-2021-09-24t1614-979695752z/53339/16 "2021-11-22T04:24:05Z")

</div>

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