%dw 2.0
output application/xml
fun normalize(date) = (date) replace "/" with "-" replace "." with "-"
---
dates: (payload.dates mapObject {
normalized_as_string: normalize($)
})
Parse Dates with DataWeave
These DataWeave examples define a function (fun
) in the DataWeave header to normalize date separators (/
, .
, and -
) within different date formats so that all of them use the same separator (-
).
Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For
DataWeave in Mule 3 apps, refer to
DataWeave version 1.2 examples.
For other DataWeave versions, you can use the version selector in the DataWeave table of contents.
The examples use these functions:
-
replace
so that all dates match a single pattern. -
mapObject
to run through thedate
elements. The script applies the normalizing function to eachdate
. -
as
(in the second DataWeave script) to change the data type of the values to a Date type with a specific date format.