period

period(period: { years?: Number, months?: Number, days?: Number }): Period

Creates a Period value as a date-based number of years, months, and days in the ISO-8601 calendar system.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

period

An object such as {years:4, months:11, days:28}. The key-value pairs are optional and can be specified in any order. An empty object ({}) returns the Period value "P0D" (zero days). The default value of each key is 0. Valid values are whole numbers, which can be positive or negative. Decimal values produce an error message. Key names are selectable.

Example

This example shows how period behaves with different inputs. The example add a subtracts and adds the result of a period function to DateTime and Date values. It also constructs a Period value from period objects and selects a months value from the object.

Source

%dw 2.0
output application/json
import * from dw::core::Periods
---
{
   dayBeforeDateTime: |2020-10-05T20:22:34.385Z| - period({days:1}),
   dayAfterDate: |2020-10-05| + period({days:1}),
   yearMonthDayAfterDate: |2020-10-05| + period({years:1, months:1, days:1}),
   emptyPeriod: period({}),
   constructPeriod: period({years:4, months:11, days:28}),
   selectMonthsFromPeriod: period({years:4, months:11, days:28}).months
}

Output

{
    "dayBeforeDateTime": "2020-10-04T20:22:34.385Z",
    "dayAfterDate": "2020-10-06",
    "yearMonthDayAfterDate": "2021-11-06",
    "emptyPeriod": "P0D",
    "constructPeriod": "P4Y11M28D",
    "selectMonthsFromPeriod": 11
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub