Search code examples
datetimemuledataweavemel

Mule: Dataweave - date formatting from strings


I've been searching around and can't find (or didn't recognize) a solution to this issue. I'm very new to MEL, so please be kind. :)

I am building a service that will accept 2 dates as strings. I want to be able to accept any (within reason) legitimate string representation of a date and coerce it to a specific format.

So far; everything I try results in errors. To somewhat simplify the process for now, I've started with a string in Dataweave instead of using the actual input for now.

I have this statement: startDate: '2016/11/25 12:00:00' as :localdatetime {format: "yyyy/MM/dd HH:mm:ss"} as :date {format: "yyyymmdd"},

I get this in response: Cannot coerce a :date to a :string, caused by :Unsupported field: MinuteOfHour.

I also tried this: startDate: '2016/11/25 12:00:00' as :date {format: "yyyymmdd"},

I get this response: Cannot coerce a :string to a :date, caused by :Text '2016/11/25 12:00:00' could not be parsed at index 4.


Solution

  • I would use:

    startDate: '2016/11/25 12:00:00' as :localdatetime {format: "yyyy/MM/dd HH:mm:ss"} as :string {format: "yyyymmdd"}
    

    Or:

    startDate: '2016/11/25 12:00:00' as :localdatetime {format: "yyyy/MM/dd HH:mm:ss"} as :localdatetime {format: "yyyymm"}
    

    Since you don't have a timezone in your initial data, then I probably wouldn't use date/datetime as they need a timezone.

    Shameless plug: I've made a video dealing with dates and DataWeave at https://www.youtube.com/watch?v=tNCqzFEq9IY&t=2s