%dw 2.0
output application/json
fun formatDate(dateString: String, inputFormat: String, outputFormat: String) =
(dateString as Date {format: inputFormat}) as String {format: outputFormat}
---
{
date1: formatDate("2023-03-15", "yyyy-MM-dd", "MM/dd/yyyy"),
date2: formatDate("2023/03/15", "yyyy/MM/dd", "dd MMM yyyy")
}
In this example, the formatDate function takes three
arguments: dateString is the input date string, inputFormat is
the format of the input date string, and outputFormat is the desired
output format. The function first converts the input date string to a Date
object using the inputFormat string, and then converts the Date
object to a string using the outputFormat string.
The output of this Dataweave expression will be a JSON
object with two fields: date1 and date2, where date1 is
the input date string “2023-03-15” converted to the output
format “MM/dd/yyyy”, and date2 is a different date
string “2023/03/15” converted to the output format “dd
MMM yyyy” using the same formatDate function