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:

Anypoint, dataweave, dataweave 2, dwl, Middleware, Mule 4, MuleCode, MuleLab, MuleProject, MuleSoft, Studio, transformation,

Anypoint, dataweave, dataweave 2, dwl, Middleware, Mule 4, MuleCode, MuleLab, MuleProject, MuleSoft, Studio, transformation,


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



By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

One thought on “How to format dates using DataWeave 2”

Leave a Reply

Your email address will not be published. Required fields are marked *