In this article we will see how to format dates as String in DataWeave 2. This is something generic code which you can use for your date transformations in dataweave 2.
For example, lets say we want the output in the format “yyyy-MM-dd’T’HH:mm:ss.SSS” to “yyyy-MM-dd“
Please find snippet below:
%dw 2.0
output application/json
fun format(df: DateTime) = df as String {format: “yyyy-MM-dd”}
—
{
CreatedDateTime: format(“2019-02-13T13:23:00.120Z”)
}
Some sample transformation preview from Anypoint studio:
For example, lets say we want the output in the format “yyyy-MM-dd’T’HH:mm:ss.SSS” to “yyyy-MM-dd“
Please find snippet below:
%dw 2.0
output application/json
fun format(df: DateTime) = df as String {format: “yyyy-MM-dd”}
—
{
CreatedDateTime: format(“2019-02-13T13:23:00.120Z”)
}
Some sample transformation preview from Anypoint studio:
You can check more information about date functions used in dataweave 2 here
Moreover, regarding date patterns when you need to format a Date as a String you using the same Java patterns as for the DateTimeFormatterBuilder
Please find the sample Mule project here
Thanks very useful .. how to transform dynamic var value…could you please also add the example for dynamic value?