如何使用 DataWeave 从 Mule 中的日期字符串中减去一天

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

我想使用 DataWeave 从 Mule 中的 date string 中减去一天:

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

作为另一种选择,我们可以按照 Date Time Operations 文档中 减去一段时间 的示例进行操作。在该示例中,我们可以定义“|”之间的句点人物。例如:|P1D|.

因此,我们可以按照以下步骤从日期中减去一天 String:

  1. 将日期 String 转换为 Date"18/03/2017 09:20:55" as :localdatetime {format: "dd/MM/yyyy HH:mm:ss"}
  2. 减去一天:[the Date on step #1] - |P1D|
  3. 日期转换回字符串[the subtracted Date on step #2] as :string {format: "dd/MM/yyyy HH:mm:ss"}

{ 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 {格式:"dd/MM/yyyy HH:mm:ss"}}