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

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

function node...

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

Ref...

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

Have a look at Jsonata date time functions

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

If you want it in milliseconds then

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

$toMillis($$.payload)

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

1 Like

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.

1 Like

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

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

Looking through stackexchange suggests this:

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;

That isn't milliseconds!

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

[{"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;

[{"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"]]}]
1 Like

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

I'll live! :grinning:

1 Like

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?

@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 :wink:

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.

This works great!
Thank you all for your assistance.

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