Manju Kaushik
Manju Kaushik

Reputation: 17

Date Difference in Hours Datetime format - Mulesoft

Need help. I have a datetime format which is received from the query parameter as

StartTime = 2021-02-20T08:33:34+0000

EndTime = 2021-03-15T04:11:56+0000

I need to get the difference in hours and I am unable to perform it. Need help please.

Tried the below DW, did not work. As there is T in between and +0000 is unable to parse it.

%dw 2.0
output application/json
var dateTime1 = payload."Start Time Stamp" as Date {format:"yyyy-MM-ddHH:mm:SSSZ"}
var dateTime2 = payload."End Time Stamp" as Date {format:"yyyy-MM-ddHH:mm:SSSZ"}
--- 
(dateTime2 - dateTime1).hours

Upvotes: 0

Views: 1134

Answers (1)

Salim Khan
Salim Khan

Reputation: 4303

You need to represent the timezone offset +0000 with ZZ.

Script:

%dw 2.0
output application/json
var dateTime1 = "2021-02-20T08:33:34+0000" as DateTime {"format":"yyyy-MM-dd'T'HH:mm:ssZZ"}
var dateTime2 = "2021-03-15T04:11:56+0000" as DateTime {"format":"yyyy-MM-dd'T'HH:mm:ssZZ"}
---
(dateTime2 - dateTime1) as Number {unit: "hours"}

Output:

547

Upvotes: 1

Related Questions