简体   繁体   中英

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
%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"} 
}

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"}

{currentdateTime:(现为:localdatetime {format:“ dd / MM / yyyy HH:mm:ss”})as:string {format:“ dd / MM / yyyy HH:mm:ss”}},beforedate:(((currentdateTime )-| P1D |)为:string {format:“ dd / MM / yyyy HH:mm:ss”}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM