This module contains functions for working and creating Periods
Functions
between
between(Date, Date): Period
Returns a Period consisting of the number of years, months,
and days between two dates.
The start date is included, but the end date is not.
The period is calculated by removing complete months, then calculating
the remaining number of days, adjusting to ensure that both have the same sign.
The number of months is then split into years and months based on a 12 month year.
A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
For example, from 2010-01-15 to 2011-03-18 is one year, two months and three days.
The result of this method can be a negative period if the end is before the start.
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
startDateInclusive |
the start date, inclusive. |
endDateExclusive |
the end date, exclusive. |
Example
This example shows how the between behaves under different inputs.
Source
1
2
3
4
5
6
7
import * from dw::core::Periods
output application/json
---
{
a: between(|2010-12-12|,|2010-12-10|),
b: between(|2010-12-10|,|2011-12-11|)
}
Output
1
2
3
4
{
"a": "P2D",
"b": "P-1Y-1D"
}
days
days(Number): Period
Create a Period that represents a given amount of days, such as 2days
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of days. It can be a decimal number |
Example
This example shows how the days behaves under different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
tomorrow: |2020-10-05T20:22:34.385Z| + days(1),
yesterday: |2020-10-05T20:22:34.385Z| - days(1)
}
Output
1
2
3
4
{
"tomorrow": "2020-10-06T20:22:34.385Z",
"yesterday": "2020-10-04T20:22:34.385Z"
}
duration
duration({ days?: Number, hours?: Number, minutes?: Number, seconds?: Number }): Period
Creates a Duration that represents a time-based amount of time. The Duration is build with the given - days : Number of days - hours : Number of hours - minutes : Number of minutes - seconds : Number of seconds
Any of the given parts can be a decimal number and the corresponding transformation is going to be done.
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
period |
The period value to be created |
Example
This example shows how the duration behaves under different inputs.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
yesterday: |2020-10-05T20:22:34.385Z| - duration({days: 1}),
dayBeforeHourBefore: |2020-10-05T20:22:34.385Z| - duration({days: 1, hours: 1}),
pointInTimeBefore: |2020-10-05T20:22:34.385Z| - duration({days: 1, hours: 1, minutes: 20, seconds: 10})
}
Output
1
2
3
4
5
{
"yesterday": "2020-10-04T20:22:34.385Z",
"dayBeforeHourBefore": "2020-10-04T19:22:34.385Z",
"pointInTimeBefore": "2020-10-04T19:02:24.385Z"
}
hours
hours(Number): Period
Create a Duration that represents a given amount of hours, such as 4hours
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of hours. It can be a decimal number |
Example
This example shows how the hours behaves under different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
nextHour: |2020-10-05T20:22:34.385Z| + hours(1),
previousHour: |2020-10-05T20:22:34.385Z| - hours(1)
}
Output
1
2
3
4
{
"nextHour": "2020-10-05T21:22:34.385Z",
"previousHour": "2020-10-05T19:22:34.385Z"
}
minutes
minutes(Number): Period
Create a Duration that represents a given amount of minutes, such as 1minutes
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of minutes. It can be a decimal number |
Example
This example shows how the minutes behaves under different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
nextMinute: |2020-10-05T20:22:34.385Z| + minutes(1),
previousMinute: |2020-10-05T20:22:34.385Z| - minutes(1)
}
Output
1
2
3
4
{
"nextMinute": "2020-10-05T20:23:34.385Z",
"previousMinute": "2020-10-05T20:21:34.385Z"
}
months
months(Number): Period
Create a Period that represents a given amount of months, such as 2months
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of months |
Example
This example shows how the months behaves under different inputs.
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
nextMonth: |2020-10-05T20:22:34.385Z| + months(1)
}
Output
1
2
3
{
"nextMonth": "2020-11-05T20:22:34.385Z"
}
period
period({ years?: Number, months?: Number, days?: Number }): Period
Creates a Period that represents date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'.
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
period |
The period object |
Example
This example shows how the period behaves under different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
import * from dw::core::Periods
---
{
lastYear: |2020-10-05T20:22:34.385Z| - period({years: 1}),
"1year1month1dayBefore": |2020-10-05T20:22:34.385Z| - duration({years: 1, months: 1, days: 1})
}
Output
1
2
3
4
{
"lastYear": "2019-10-05T20:22:34.385Z",
"1year1month1dayBefore": "2019-09-04T20:22:34.385Z"
}
seconds
seconds(Number): Period
Create a Duration that represents a given amount of seconds, such as 1second
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of seconds. It can be a decimal number |
Example
This example shows how the seconds behaves under different inputs.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
nextSecond: |2020-10-05T20:22:34.385Z| + seconds(1),
previousSecond: |2020-10-05T20:22:34.385Z| - seconds(1)
}
Output
1
2
3
4
{
"nextSecond": "2020-10-05T20:22:35.385Z",
"previousSecond": "2020-10-05T20:22:33.385Z"
}
years
years(Number): Period
Create a Period that represents a given amount of years, such as 3years
_Introduced in DataWeave 2.4.0. Supported by Mule 4.4.0 and later._
Parameters
| Name | Description |
|---|---|
nYears |
The number of years |
Example
This example shows how the years behaves under different inputs.
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Periods
output application/json
---
{
nextYear: |2020-10-05T20:22:34.385Z| + years(1)
}
Output
1
2
3
{
"nextYear": "2021-10-05T20:22:34.385Z"
}