'Date to DateTime conversion in DataWeave 2

Input date is coming in this format "2022-04-30" and I want to convert it to date time and time should be zeros like 2022-05-15T00:00:00Z.

How it can be achieved using DataWeave 2.0?



Solution 1:[1]

Convert the input string to a Date, then add the time to create a DateTime output. Then just convert to String. The default pattern seem to be adequate that you need.

%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate as Date {format: "yyyy-MM-dd"} ++ |00:00:00Z|) as String 

Output:

"2022-04-30T00:00:00Z"

Solution 2:[2]

If you want the result as String you can just Append "T00:00:00Z" to your date string

%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
sourceDate ++ "T00:00:00Z"

And if you want a DateTime Object you can just coerce it to DateTime using as keyword. It will work as your date string is already in the required format.

%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate ++ "T00:00:00Z") as DateTime

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aled
Solution 2