Forum Discussion

Sahil's avatar
Sahil
Contributor
5 years ago
Solved

Need to convert yyyymmdd to yyyy-mm-dd

Hi,
I have a element as:-
jsonPath($, “$Z_ORD.IDOC.DK02[].DATE")
and value is coming as 20210521(yyyymmdd).
I need to convert it to 2021-05-21 and map it to target ml element.
I am using mapper. Tried like below:-
Date.parse(jsonPath($, "$Z_ORD.IDOC.DK02[
].DATE”,“yyyMMdd”)).toLocaleDateTimeString({“format”:“yyyy-MM-dd”}) || null

but getting error as:-
Not-a-number (NaN) does not have a method named: toLocaleDateTimeString, found in: …:“yyyy-MM-dd”}). Perhaps you meant: toString, toExponential, toPrecision, toFixed

  • I ment at the end of my expression which parsed your date.

    Date.parse($Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(0,4)).concat($Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(4,6)),$Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(6))).join(“-”)).toLocaleDateString()

7 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hi @Sahil,

    I presume the jsonPath should look like this : jsonPath($, “$Z_ORD.IDOC.DK02[*].DATE”). The value from this jsonPath is an array, so to get it, you have to go with .toString() or with indexing [0]. Even than, Date.parse() would return incorrect date, because 20210521 will be treated as an integer of milliseconds since midnight January 1, 1970 UTC. You can use one of the following expressions to get the right format:

    Date.parse([…jsonPath($, “$Z_ORD.IDOC.DK02[].DATE").toString().substring(0,4),jsonPath($, "$Z_ORD.IDOC.DK02[].DATE”).toString().substring(4,6),jsonPath($, “$Z_ORD.IDOC.DK02[*].DATE”).toString().substring(6)].join(“-”))

    Date.parse($Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(0,4)).concat($Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(4,6)),$Z_ORD.IDOC.DK02.map(x=>x.DATE.toString().substring(6))).join(“-”))

    Hope this helps,
    Regards,
    Bojan

    • Sahil's avatar
      Sahil
      Contributor

      Hi,
      Thank you for the reply.
      1st option was throwing error so I tried 2nd one but getting date as
      2021-03-17T00:00:00.000 UTC

      it should be just, please let me know how do I rectify it.
      2021-03-17