DataWeave: Play With Dates (Part 1) – Beragampengetahuan
3 mins read

DataWeave: Play With Dates (Part 1) – Beragampengetahuan

DataWeave is a very powerful language provided by MuleSoft to transform data. It is the primary language of MuleSoft for data transformation and an expression language for components and connectors configuration.

This is part 1 of the series of blogs, which gives very good insights on the very basic date operations or functions that a developer would be using on a regular basis.

Contents

1. Find the Number of Days Between Two Days

The daysBetween DataWeave function can be used to find the number of days between any two dates. This can be applied in scenarios where you need to find the number of days between the start and end dates of a membership/contract. The data type of the output generated by this function is “Number.”

This function accepts two mandatory parameters, and the type of parameters should always be either in date or date time.

This function does not accept null or empty strings. It gives an error if any values are provided as input parameters other than date or date times.

Input

{
    "membership": {
        "startDate": "27-05-2023",
        "endDate": "27-06-2025"
    }
}

DataWeave Expression

%dw 2.0
output application/json
---
{
"numberOfDays" : daysBetween((payload.membership.startDate as Date {format:"dd-MM-yyyy"}), payload.membership.endDate as Date {format:"dd-MM-yyyy"})
}

Output

2. Find if a Given Date or Datetime Is a Leap Year

The isLeapYear DataWeave function can used to find if any given date or datetime is a leap year. This function gives true if a date or datetime is a leap year; otherwise, it returns false. This can be applied in scenarios where a company plans to provide any discounts or offers for the membership fee if the membership is being created in a leap year. The data type of the output generated by this function is “Boolean.”

This function accepts a mandatory parameter, and the type of the parameter should always be either in date or date time.

This function does not accept null or empty strings. It gives an error if any values are provided as input parameters other than date or date times.

DataWeave Expression

%dw 2.0
output application/json
---
{
    "leapYearTest1" : isLeapYear(now()),
    "leapYearTest2" : isLeapYear("27-06-2025" as Date {format:"dd-MM-yyyy"}),
    "leapYearTest2" : isLeapYear("2023-09-23T13:59:35.340539Z")
}

Output

{
  "leapYearTest1": true,
  "leapYearTest2": false,
  "leapYearTest2": false
}

3. Add Days to the Current Date or Any Provided Date

The DataWeave examples below show multiple ways to add days to a date and datetime. All the dataweave examples added below are for dataweave 2.x, which is only available in Mule 4.x.

The examples below use:

  • as function to coerce a String to Period type
  • P<date>T<time> for the Period data type, which provides designators for years, months, days, hours, minutes, and seconds
    For example, |P2Y9M1D| refers to a period of two years, nine months, and one day, and |PT5H4M3S| indicates a time period of five hours, four minutes, and three seconds.

DataWeave Expression

%dw 2.0
output application/json
var numberOfDays = 3
---
{
  oneDayAfter: |2023-10-01T23:57:59Z| + |P1D|,
  threeDaysAfter: |2023-10-01T23:57:59Z| + ("P$(numberOfDays)D" as Period),
  a: |2020-10-01| + |P1D|,
  b: |P1D| + |2020-10-01|,
  c: now() + |P1D|
}

rencana pengembangan website



metode pengembangan website

jelaskan beberapa rencana untuk pengembangan website, proses pengembangan website, kekuatan dan kelemahan bisnis pengembangan website
, jasa pengembangan website, tahap pengembangan website, biaya pengembangan website

#DataWeave #Play #Dates #Part

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *