Dataweave 2.4.0 Dates Module Functions
This blog gives the details of the dw::core::Dates module functions which are released in DataWeave version 2.4.0 for Mule Version 4.4.
Join the DZone community and get the full member experience.
Join For FreeDataWeave is a programming language designed for transforming data. It is the primary language of MuleSoft for data transformation and an expression language for components and connectors configuration.
Mulesoft released Dataweave 2.4.0 for Mule Version 4.4. The 2.4.0 version of DataWeave introduced many new features. In this post, we will see one of the newly introduced DataWeave modules.
dw::core::Dates is a new module that contains functions for creating and manipulating dates.
To use functions of this module, we need to import it in our Dataweave code as below:
import * from dw::core::Dates
dw::core::Dates has below functions:
1. atBeginningOfDay
This function is used to change the Time value in the input DateTime to the beginning of the specified day which means hours, minutes, and seconds in the input change to 00:00:00.
Note: This function only accepts the input of type DateTime and LocalDateTime.
Input:
{
"inputDate" : "2021-10-06T21:42:46"
}
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate : atBeginningOfDay(payload.inputDate)
}
Output:
{
"outputDate": "2021-10-06T00:00:00Z"
}
2. atBeginningOfHour
This function is used to changes the Time value in the input DateTime/Time value to the beginning of the specified hour means minutes and seconds in the input change to 00:00.
Note: This function only accepts the input of type DateTime, LocalDateTime, LocalTime, Time.
Input:
{
"inputDate" : "2021-10-06T21:42:46"
}
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate : atBeginningOfHour(payload.inputDate)
}
Output:
{
"outputDate": "2021-10-06T21:00:00Z"
}
3. atBeginningOfMonth
This function is used to change the Day value from the input DateTime value to the first day of the specified month.
Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it also sets the Time value to 00:00:00.
Input:
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" : "2021-10-06"
}
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime : atBeginningOfMonth(payload.inputDateTime),
outputDate : atBeginningOfMonth(payload.inputDate),
}
Output:
{
"outputDateTime": "2021-10-01T00:00:00Z",
"outputDate": "2021-10-01"
}
4. atBeginningOfWeek
This function is used to change the Day and Time values of the input DateTime value to the beginning of the first day of the week. The function treats Sunday as the first day of the week.
Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it sets the Time value to 00:00:00.
Input:
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" : "2021-10-06"
}
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime : atBeginningOfWeek(payload.inputDateTime),
outputDate : atBeginningOfWeek(payload.inputDate)
}
Output:
{
"outputDateTime": "2021-10-03T00:00:00Z",
"outputDate": "2021-10-03"
}
5. atBeginningOfYear
This function is used to change the input DateTime value to the first day of the year.
Note: This function only accepts the input of type DateTime, LocalDateTime, Date. If the input has a Time value then it sets the Time value to 00:00:00.
Input:
{
"inputDateTime" : "2021-10-06T21:42:46",
"inputDate" : "2021-10-06"
}
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDateTime : atBeginningOfYear(payload.inputDateTime),
outputDate : atBeginningOfYear(payload.inputDate)
}
Output:
{
"outputDateTime": "2021-01-01T00:00:00Z",
"outputDate": "2021-01-01"
}
6. date
This function is used to create a Date value by passing values specified for the year, month, and day fields.
month -> Can have any value between 1 to 12
day -> Can have any value between 1 to 31
year -> Can have any value between 1000 to 9999
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate : date({year: 2021, month: 10, day: 11})
}
Output:
{
"outputDate": "2021-10-11"
}
7. dateTime
This function is used to create a DateTime value by passing values specified for the year, month, day, hour, minutes, seconds, and timezone fields.
month -> Can have any value between 1 to 12
day -> Can have any value between 1 to 31
year -> Can have any value between 1000 to 9999
hour -> Can have any value between 0 to 23
minutes -> Can have any value between 0 to 59
seconds -> Can have any value between 0 to 59.99
timezone -> Should be passed in time offset format. E.g., |+05:30| , |-03:00| etc.
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: dateTime({year: 2021, month: 10, day: 11, hour: 10, minutes: 20, seconds: 30 , timeZone: |+05:00|})
}
Output:
{
"outputDate": "2021-10-11T10:20:30+05:00"
}
8. localDateTime
This function is used to create a LocalDateTime value by passing values for the year, month, day, hour, minutes, and seconds fields.
month ->Can have any value between 1 to 12
day -> Can have any value between 1 to 31
year -> Can have any value between 1000 to 9999
hour -> Can have any value between 0 to 23
minutes -> Can have any value between 0 to 59
seconds -> Can have any value between 0 to 59.99
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: localDateTime({year: 2021, month: 10, day: 11, hour: 10, minutes: 20, seconds: 30})
}
Output:
{
"outputDate": "2021-10-11T10:20:30"
}
9. localTime
This function is used to create a LocalTime value by passing values for the hour, minutes, and seconds fields. Valid values for these fields are as following:
hour -> Can have any value between 0 to 23
minutes-> Can have any value between 0 to 59
seconds -> Can have any value between 0 to 59.99
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: localTime({ hour: 10, minutes: 20, seconds: 30})
}
Output:
{
"outputDate": "10:20:30"
}
10. time
This function is used to create a Time value by passing values for the hour, minutes seconds, and timezone fields. Valid values for these fields are as following :
hour -> Can have any value between 0 to 23
minutes -> Can have any value between 0 to 59
seconds -> Can have any value between 0 to 59.99
timezone -> Should be passed in time offset format. E.g., |+05:30| or |-03:00|
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
outputDate: time({ hour: 10, minutes: 20, seconds: 30 , timeZone: |+05:30|})
}
Output:
{
"outputDate": "10:20:30+05:30"
}
11. today
This function gives today’s date as a Date type in response.
12. tomorrow
This function gives tomorrow’s date as a Date type in response.
13. yesterday
This function gives yesterday’s date as a Date type in response.
DataWeave Expression:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
"todayDate" : today(),
"tomorrowDate" : tomorrow(),
"yesterdayDate" : yesterday()
}
Output:
{
"todayDate": "2021-10-07",
"tomorrowDate": "2021-10-08",
"yesterdayDate": "2021-10-06"
}
This is how we can use dw::core::Date module functions. For more details please check Mulesoft official documentation here.
Opinions expressed by DZone contributors are their own.
Comments