<dates>
<date>26-JUL-16</date>
<date>27/JUL/16</date>
<date>28.JUL.16</date>
</dates>
Parse Dates with DataWeave
DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
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 (-
).
The examples use these functions:
-
replace
so that all dates match a single pattern. -
mapObject
to run through the 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.
Example: Returns Dates as String Types
%dw 2.0
output text/xml
fun normalize(date) date replace "/" with "-" replace "." with "-"
---
dates: (payload.dates mapObject {
normalized_as_string: normalize($.date)
})
<?xml version='1.0' encoding='US-ASCII'?>
<dates>
<normalized_as_string>26-JUL-16</normalized_as_string>
<normalized_as_string>27-JUL-16</normalized_as_string>
<normalized_as_string>28-JUL-16</normalized_as_string>
</dates>
Example: Returns Dates as Date Types
<dates>
<date>26-JUL-16</date>
<date>27/JUL/16</date>
<date>28.JUL.16</date>
</dates>
%dw 2.0
output text/xml
fun normalize(date) date replace "/" with "-" replace "." with "-"
---
// Outputs date values as Date types in the specified format
dates: (payload.dates mapObject {
normalized_as_date: normalize($.date) as Date {format: "d-MMM-yy"}
})
<?xml version='1.0' encoding='US-ASCII'?>
<dates>
<normalized_as_date>2016-07-26</normalized_as_date>
<normalized_as_date>2016-07-27</normalized_as_date>
<normalized_as_date>2016-07-28</normalized_as_date>
</dates>