Contact Free trial Login

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 DataWeave 2.0 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

The examples use these functions:

  • replace so that all dates match a single pattern.

  • mapObject to run through the the date elements. The script applies the normalizing function to each date.

  • 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

Input
<dates>
  <date>26-JUL-16</date>
  <date>27/JUL/16</date>
  <date>28.JUL.16</date>
</dates>
DataWeave
%dw 2.0
output text/xml
fun normalize(date) date replace "/" with "-" replace "." with "-"
---

dates: (payload.dates mapObject {
    normalized_as_string: normalize($.date)
})
Output (dates are String types)
<?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

Input
<dates>
  <date>26-JUL-16</date>
  <date>27/JUL/16</date>
  <date>28.JUL.16</date>
</dates>
DataWeave
%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"}
})
Output (dates are Date types)
<?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>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.