简体   繁体   中英

DateWeaver date field conversion not working - Mule

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

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.

 %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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM