How to format dates 📆 in Dataweave 2.0 Mule 4

When you need to format a Date as a String you using the same Java patterns as for the DateTimeFormatterBuilder

For example, let's say we want the output in the format yyyy-MM-dd’T’HH:mm:ss.SSS 

Date Time conversion examples in Mule 4 Dataweave 2.0



Dataweave Code:
%dw 2.0
output application/json
fun format(d: DateTime) = d as String {format: "yyyy-MM-dd'T'HH:mm:ss.SSS"}
---
{
CreatedDateTime: format(|2019-02-13T13:23:00.120Z| >> "CET"),
}

Result:
{
"CreatedDateTime": "2019-02-13T14:23:00.120"

Examples: 
%dw 2.0
output application/json
var utc = "UTC" as TimeZone
fun printFormattedDate()=(now() >> utc) as String
---
{
myDate: printFormattedDate()
}

Result:
{
"myDate": "2019-09-15T23:08:56.367Z"
}

Others:
"Date of Birth" : ($."DOB" as :date) as :string {format: "yyyy-MM-dd HH:mm:ss"}


Want to learn Mulesoft and ESB tutorials. Our blog provides the best articles on Mule4, Mule 3, Dataweave, Anypoint platform, and Anypoint Studio. We provide tutorials on creating mule applications, how to use different connectors, running DataWeave code, and solving issues. Let us know if you want to create a tutorial on any Mulesoft Tutorials


Post a Comment

5 Comments

  1. How to convert number to HH:mm:ss alone

    example : payload { time: 101122}
    output : 10:11:22

    ReplyDelete
  2. Please try
    payload.time as DateTime {format: "hh:mm:ss"}

    ReplyDelete
  3. yes, it is converted to the expected format. But value got change.

    Expected value should be same as the payload value. Ex : payload { time:123456}
    output: 12:34:56
    if you observer in output value, just added ":" after every 2 digits

    ReplyDelete
  4. yes, it is converted to the expected format. But value got change.

    Expected value should be same as the payload value. Ex : payload { time:123456}
    output: 12:34:56
    if you observer in output value, just added ":" after every 2 digits

    ReplyDelete
  5. Can you please check mail

    ReplyDelete