Thiru
Thiru

Reputation: 424

date operations in mule esb using data weave

I have requirement like where need to find difference in between two dates using dataweave, Both input and output is XML format.

both the date formats are yyyy.mm.dd and output date format must be like mm.dd.yy or mm.dd.yyyy.

Please assist me, thanks

Upvotes: 0

Views: 2145

Answers (4)

Srinivas
Srinivas

Reputation: 92

Subtracting two Dates in dataweave. Format the date according to your requirement

%dw 1.0
%output application/json
---
{
  a: |23:59:56-03:00| - |22:59:56-00:00|,
  b: |2003-10-01| - |2002-09-23|
}

{
  "a": "PT-4H",
  "b": "P-1Y-8D"
}

Upvotes: 0

Abhay
Abhay

Reputation: 324

In Dataweave, you can convert a string to date format and then just subtract them

Input

<dates>
   <startDate>2007.05.01</startDate>
   <endDate>2017.02.15</endDate>
</dates>

Transform

%dw 1.0
%var startDate = payload.dates.startDate as :date {format: "yyyy.MM.dd"}
%var endDate = payload.dates.endDate as :date {format: "yyyy.MM.dd"}
%output application/json
---
{
    difference: startDate - endDate
}

Output

{
   "difference": "P9Y9M14D"
}

9 years, 9 months, 14 days

Upvotes: 1

Abani Patra
Abani Patra

Reputation: 172

You can create a global function with Java/Groovy and use DateTime object to find the difference and return from that function. Now you can use that function inside your dataweave.

Upvotes: 0

JoostD
JoostD

Reputation: 744

You can format dates like this (example):

yourInputDate as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ss"})

you can add and subtract dates, another example how i use this in a project with variables:

%var stamp = (now as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ss"})
%var dayDiff = ("P" ++ (stamp.dayOfWeek - 1) ++ "D") as :period
%var firstDateWeek = (stamp - dayDiff) as :localdatetime {format: "yyyy-MM-dd"}

docs here: https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#datetime

Upvotes: 1

Related Questions