1

In DataWeaver documentation 10.8. Changing the Format of a Date https://developer.mulesoft.com/docs/dataweave#_date_time_operations

Below is the transform 

 %dw 1.0
 %output application/json
 %type mydate = :string { format: "YYYY/MM/dd" }
 ---
{
formatedDate1: |2003-10-01T23:57:59| as :mydate,
formatedDate2: |2015-07-06T08:53:15| as :mydate
}

In the dataweaver preview it is looking fine as expected response ( Changed the date format). I'm taking response in file component, But it is not converting the date in the format mentioned( Also kept logger right after the dataWeaver, not an expected response).

Response getting as below

{
"formatedDate1": "2003-10-01T23:57:59",
"formatedDate2": "2015-07-06T08:53:15"
 }

I have other query, here we are hardCoding the date inside the weaver. If suppose we are taking the date field from Input parameter does we need to wrap the field inside ||. Example as below, will it work

    %dw 1.0
    %output application/json
     %type mydate = :string { format: "YYYY/MM/dd" }
    ---
    {
     formatedDate1: |payload.dateField1| as :mydate,
     formatedDate2: payload.dateField1 as :mydate
    }

The above seems not to work for me. Please let me know the correct usage. Thanks in advance

DavoCoder
  • 862
  • 6
  • 17
star
  • 1,493
  • 1
  • 28
  • 61

2 Answers2

2

Try this:

%dw 1.0
%output application/json
%type mydate = :date { format: "yyyy/M/d" }
---
{
  formatedDate1: |2003-10-01T23:57:59| as :mydate,
  formatedDate2: |2015-07-06T08:53:15| as :mydate
}

Output:

{
  "formatedDate1": "2003-10-01",
  "formatedDate2": "2015-07-06"
}

The difference is the datatype from :string to :date::

%type mydate = **:date** { format: "yyyy/M/d" }

It seems the result doesn't change to /. This is probably a bug.

perror
  • 7,071
  • 16
  • 58
  • 85
Ralph Rimorin
  • 329
  • 2
  • 10
  • Thanks!! Correct should be a bug!!. Do you have any idea on my another query. How to pass date field from inputField without handcoding it inside transformation. Hope you got my question correctly. – star Sep 23 '15 at 02:20
  • Yes I got it. That is also a limitation since I can't find any parser in Dataweave to cast the text formatDate1 to :date. Maybe you can try is to unmarshall the JSON to a pojo (json to Object, with the return class containing the two date fields.) then add that as an input in dataweave, i.e. adding to its metadata. – Ralph Rimorin Sep 23 '15 at 03:26
  • It would be great if DataWeaver directly can provided the functionality. – star Sep 23 '15 at 03:51
  • Certainly, you can create a bug report in Jira and request that functionality. – Ralph Rimorin Sep 23 '15 at 03:58
0
 %dw 1.0
 %output application/json
 %type mydate = :string { format: "YYYY/M/d" }
 ---
{
formatedDate1: |2003-10-01T23:57:59| as :mydate,
formatedDate2: |2015-07-06T08:53:15| as :mydate
}

Try this

Satheesh Kumar
  • 797
  • 7
  • 31
  • Have tried. Only can see the correct response in Preview. But in the file output same what is printed in the dataweaver is the response. Do I need to change any config setting for DataWeaver( But there is not such settings). – star Sep 22 '15 at 00:59