Dates in Dataweave

Dates in Dataweave

Dataweave has a rich set of Date based operations.

Here, I will cover a few.

Printing current Date:
%dw 2.0
output application/json
---
{
    currentDate: now()
}

now() prints the current DateTime

Formatting Dates,

Let's say you want to print dates in format: day-month-year

%dw 2.0
output application/json
---
{
    currentDate: now() as String{format: 'dd-MM-uuuu'}
}

The above code prints the date in the format. Check I have used 'dd-MM-uuuu'. You can also use 'dd-MM-yyyy'

%dw 2.0
output application/json
---
{
    currentDate: now() as String{format: 'dd-MM-yyyy'},
}
Changing Time Zone
%dw 2.0
output application/json
---
{
    currentDate: now() >> "America/New_York"
}

This will change the time zone to American/New_York

Another method of changing time zone:

%dw 2.0
output application/json
---
{
    currentDate: now() >> |-08:00|
}

This will change time zone to Pacific Time.

Adding Time
  1. Using Period
%dw 2.0
fun addMinutes(date, minutes: String) =
    date + ("PT$(minutes)M" as Period)
output application/json
---
{
    currentDate: now(),
    currentDateaddMinutes: addMinutes(now(), 1 as String)
}

2. Using Milliseconds

%dw 2.0
output application/json
---
{
    currentDate: now() as String {format:"yyyy/dd/MM HH:mm:ss"},
    currentDateinMillis: (now() + 1),
    currentDateaddMillis: (now() + 1) as  DateTime as String {format:"yyyy/dd/MM HH:mm:ss"}
}

Hope you like it!