cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Problems with Date.parse() function

VictorC
New Contributor

Hi, I have a problem with the Date.parse () function.
I cannot convert a String to date, the format of my string is the following: โ€œdd/MM/yyyyโ€.
It only happens to me with some specific dates (with most data I have no problem).
The problem occurs with the following examples:
Date.parse (โ€œ01/04/1932โ€, โ€œdd/MM/yyyyโ€)
Date.parse (โ€œ05/02/1939โ€, โ€œdd/MM/yyyyโ€)
Date.parse (โ€œ09/12/1940โ€, โ€œdd/MM/yyyyโ€)
Date.parse (โ€œ16/12/1943โ€, โ€œdd/MM/yyyyโ€)
Date.parse (โ€œ12/02/1950โ€, โ€œdd/MM/yyyyโ€)
And I always get a NaN as a result of the function.
Thank you.

10 REPLIES 10

davidm
New Contributor

I have a similar issue. When I use literal string, it works. But using a field, it does not. Additionally, it only happened yesterday 2021-03-14.

This fails.
snaplogic

This works.
Screen Shot 2021-03-15 at 2.27.11 PM

davidm
New Contributor

Date.parse(โ€œ2021-03-14 02:40:18.000โ€,โ€œyyyy-MM-dd HH:mm:ss.SSSโ€)

Use this on my mapper and it worked. However, when I use same approach on my existing snaplogic job coming from a field and it fails. I will just do date manipulation inside SQL as standard functions in Snaplogic is buggy.

When you have time, could you give an example of the string format and date where it fails?

davidm
New Contributor

Sure. It is actually the same date in my reply. It only fails because it returns NaN. The weird about it is that I have a similar job running the same way and it does not fail.

Date.parse(โ€œ2021-03-14 02:40:18.000โ€,โ€œyyyy-MM-dd HH:mm:ssโ€+โ€œ.000โ€) - the difference is the .000 at the end.

So the format string should include the milliseconds or the parse fails with NaN.

Could you try this format with the milliseconds on the end?

Date.parse("2021-03-14 02:40:18.000", "yyyy-MM-dd HH:mm:ss.SSS")

Hereโ€™s a pipeline to play around with the two different parse examples.

date-parse-example_2021_03_16.slp (2.3 KB)