MuleSoft Technical Guides

Date formatting in DataWeave 2.0

User MuleSoft Integration Team
Calendar February 23, 2021

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.

  1. Date: Date represented in yyyy-MM-dd format. E.g: 2020-10-17
  2. DateTime: Conjunction of Date and time in yyyy-MM-ddTHH:mm:ss format with timezone. E.g: 2020-10-17T23:57:59+05:30
  3. LocalDateTime: Conjunction of Date and time in yyyy-MM-ddTHH:mm:ss format without timezone.E.g: 2020-10-17T23:57:59
  4. LocalTime: Time in the current timezone.E.g: 23:57:59
  5. Timezone: Time relative to GMT. Must start with a + or – . E.g: +05:30
  6. 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.

Leave a comment

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