Problem with parse a string to DateTime

#1

Hello,
from an application I get the date in the format "20111230T213000Z", how can I convert it in a function to a DateTime object to calculate with it etc.?
var d = new Date('20111230T213000Z') does not work.

Holger

0 Likes

#2

It tends to be time string but somehow proper format is lost .
The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ
If you cant get properly formatted string from source, you need to break this string into parts to make your time object. There is many ways to do this.
As you are already using the function node, so do it by using string extracting methods.
https://www.w3schools.com/js/js_string_methods.asp

0 Likes

#3

Hello hotNipi,
thanks for your fast answer. I tried it:

var d = new Date('2019-02-06T05:00:00.000Z');
var year = d.getYear();  
var month = d.getMonth();

but year is "119" and month ist "1"

Holger

0 Likes

#4

It might be worth re-reading https://www.w3schools.com/js/js_date_methods.asp

1 Like

#5

Yes, it works, thank you very much.
Holger

0 Likes

#6

Also check out the node-red-contrib-moment node if you need a node for date/time conversions and simple calculations.

0 Likes