简体   繁体   中英

MULE how to calculate number of days between two dates in dataweave

I have two dates in JSON response "startDate": "2017-04-30", "EndDate": "2017-05-27",

they are transformed in dataweave as dates. I need to calculate number of days between those two dates. Is there any function in dataweave to do it? Thanks

Here you go:

  {
     "diff": ((payload.start_date as :date) - (payload.end_date as :date)).days
   }

屏幕截图

建议使用MEL全局函数并使用ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant())

Adding to the answer by @Dinesh , The returned object from subtracting dates is a duration object which you can perform different methods on it. More about Duration class

%dw 2.0 output application/json

fun getDiffDays(date1: String, date2: String) = ( (date1 as Date) - (date2 as Date)).days


{

"diff4":        getDiffDays("2021-10-04", "2020-10-04"),

}

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