CooperShelly
CooperShelly

Reputation: 123

How to substract one day from a date string in Mule using DataWeave

I want to substract one day from a date string in Mule using DataWeave :

Exemple:

Input date : 18/03/2017 09:20:55
Output date : 17/03/2017 09:20:55

Upvotes: 0

Views: 4587

Answers (3)

veda
veda

Reputation: 10

{ currentdateTime: (now as :localdatetime {format : "dd/MM/yyyy HH:mm:ss"})as :string {format : "dd/MM/yyyy HH:mm:ss"}, beforedate: ((currentdateTime) - |P1D|)as :string {format : "dd/MM/yyyy HH:mm:ss"} }

Upvotes: 0

sulthony h
sulthony h

Reputation: 1277

As another alternative, we can follow the example from Date Time Operations documentation for Subtracting a Period of Time. In that example we can defining the period between '|' characters. For example: |P1D|.

Therefore, we can do the following steps to subtract one day from a date String:

  1. Transform the date String to a Date: "18/03/2017 09:20:55" as :localdatetime {format: "dd/MM/yyyy HH:mm:ss"}
  2. Subtract one day: [the Date on step #1] - |P1D|
  3. Transform the Date back to String: [the subtracted Date on step #2] as :string {format: "dd/MM/yyyy HH:mm:ss"}

Upvotes: 1

Alagappan
Alagappan

Reputation: 66

%dw 1.0
%output application/json
%var aPeriod=("P" ++ 1 ++ "D") as :period
%var cDatetime= now as :datetime {format: "MM/dd/yyyy HH:mm:ss"} 
---
{
previousDate: (cDatetime - aPeriod) as :datetime {format: "MM/dd/yyyy HH:mm:ss"} 
}

Upvotes: 1

Related Questions