Date formatting in DataWeave 2.0

We often encounter scenarios to convert dates from one format to another, and I find the date formatting a massive task in itself. Date conversion becomes essential in a state of affairs where there is a need to sync two databases/systems. It’s not an easy task since most databases/systems have their specific formats. Maybe the client wants a date in some particular format; hence, the responsibility to convert from one format to another falls upon Mule or, say, falls upon us, MuleSoft developers. We’ll explore on conveniently change the date formatting in DataWeave.
Let’s start simple and understand the native date types in DataWeave Format.
- Date: Date represented in yyyy-MM-dd format. E.g: 2020-10-17
- DateTime: Conjunction of Date and time in yyyy-MM-ddTHH:mm:ss format with timezone. E.g: 2020-10-17T23:57:59+05:30
- LocalDateTime: Conjunction of Date and time in yyyy-MM-ddTHH:mm:ss format without timezone.E.g: 2020-10-17T23:57:59
- LocalTime: Time in the current timezone.E.g: 23:57:59
- Timezone: Time relative to GMT. Must start with a + or – . E.g: +05:30
- Period: Represents an amount of time in following form P[n]Y[n]M[n]DT[n]H[n]M[n]S. It consists of two parts – Date period, the part before T and Duration, the part after T
Below is the DataWeave script to check the native date types in DataWeave:
{
Date: typeOf(|2020-10-17|),
DateTime: typeOf(|2020-10-17T23:57:59+05:30|),
LocalDateTime: typeOf(|2020-10-17T23:57:59|),
LocalTime: typeOf(|23:57:59|),
TimeZone: typeOf(|+05:30|),
Period: typeOf(|P1Y|)
}
Below is the Output for the script above:
{
"Date": "Date",
"DateTime": "DateTime",
"LocalDateTime": "LocalDateTime",
"LocalTime": "LocalTime",
"TimeZone": "TimeZone",
"Period": "Period"
}
To get the current time of the system, Dataweave offers now() function.
Printing now() will output:
"2021-02-11T15:04:31.277+05:30"
The details like day, month, year etc., are facilitated by now() function.
{
"Current Date and Time": now(),
"Year": now().year,
"Quarter": now().quarter,
"Month": now().month,
"Day": now().day,
"Hour": now().hour,
"Minutes": now().minutes,
"Seconds": now().seconds,
"Day Of Week": now().dayOfWeek,
"Day Of Year": now().dayOfYear
}
Below is the output for the script above:
{
"Current Date and Time": "2021-02-11T09:37:14.406Z",
"Year": 2021,
"Quarter": 1,
"Month": 2,
"Day": 11,
"Hour": 9,
"Minutes": 37,
"Seconds": 14,
"Day Of Week": 4,
"Day Of Year": 42
}
Formatting Dates:
DataWeave can help us attain dates in different formats than what now() returns. Please note that the below script is just an example of the different date formats and doesn’t encompass all the possible scenarios.
%dw 2.0 output application/json --- { "Current Date and Time": now(), "LocalDateTime": now() as LocalDateTime, "Date in dd-MM-yyyy format": now() as Date {format: "dd-MM-yyyy"}, "Date in dd-MMM-yyyy format": now() as Date {format: "dd-MMM-yyyy"}, "Date in dd/MM/yyyy format": now() as Date {format: "dd/MM/yyyy"}, "Date in dd/MMM/yyyy format": now() as Date {format: "dd/MMM/yyyy"}, "Date in dd E, MMM, yyyy format": now() as Date {format: "dd E, MMM, yyyy"}, "Date in yyyy/MMM/dd format": now() as Date {format: "yyyy/MMM/dd"}, "Date in dd/MMM/yyyy format": now() as Date {format: "dd/MMM/yyyy"}, //Instead of HH and hh, kk and KK can be used respectively "Date in 24hr Format" : now() as String {format : "dd-MM-yyyy HH:mm:ss"}, "Date in 12hr Format" : now() as String {format : "dd-MM-yyyy hh:mm:ss a"} }
Output:
{
"Current Date and Time": "2021-02-11T09:38:40.959Z",
"LocalDateTime": "2021-02-11T09:38:40.959",
"Date in dd-MM-yyyy format": "11-02-2021",
"Date in dd-MMM-yyyy format": "11-Feb-2021",
"Date in dd/MM/yyyy format": "11/02/2021",
"Date in dd/MMM/yyyy format": "11/Feb/2021",
"Date in dd E, MMM, yyyy format": "11 Thu, Feb, 2021",
"Date in yyyy/MMM/dd format": "2021/Feb/11",
"Date in dd/MMM/yyyy format": "11/Feb/2021",
"Date in 24hr Format": "11-02-2021 09:38:40",
"Date in 12hr Format": "11-02-2021 09:38:40 AM"
}
Here, we saw a few examples of formatting system dates and dates received as part of payload into different formats and how to extract information from dates.
*There can be many more scenarios for date conversions other than the one in this MuleSoft technical guide.
Recent Blogs

AI-Driven PDF Parsing in Salesforce
Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce
AI-Driven PDF Parsing in Salesforce
Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce

Compression Namespace in Apex: A Powerful New Salesforce Feature
Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature
Compression Namespace in Apex: A Powerful New Salesforce Feature
Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature

Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing
Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers