I have a requirement to combine date which is in this format "2021-09-04T00:00:00-05:00" and time which is a String "37320000" to combine these two to form this format "yyyy-MM-ddTHH:mm:ss.SSSZ" the default value ss.SSS is 00.000. Is this possible to achieve using dataweave 2.0 in Mule ?
CodePudding user response:
Used your example and here is the dw to achieve the required format. Try here in online
CodePudding user response:
I found this method Script:
%dw 2.0
output application/json
fun addElapsedToDate(date, elapsedMs) =
do {
var s=(elapsedMs / 1000) mod 60
var m=floor(elapsedMs / (1000*60)) mod 60
var h=floor(elapsedMs / (1000*60*60)) mod 24
var ms=elapsedMs mod 1000
---
date "PT$(h)H" as Period "PT$(m)M" as Period "PT$(s)S" as Period
}
var date="2021-09-04T00:00:00-05:00"
var duration="37320001"
---
(addElapsedToDate(date as DateTime {format: "yyyy-MM-dd'T'HH:mm:ssxxx"}, duration as Number))
as String {format:"yyyy-MM-dd'T'HH:mm:ss.SSSZ"}
Output:
"2021-09-04T10:22:00.001-0500"
If you are using Mule 4.4 you can use the new function Periods::seconds() to achieve the same result:
%dw 2.0
output application/json
import * from dw::core::Periods
var date="2021-09-04T00:00:00-05:00"
var duration="37320001"
---
date as DateTime {format: "yyyy-MM-dd'T'HH:mm:ssxxx"}
seconds((duration as Number)/1000)