---
schema_version: "1"
id: 9bc0d0e7de34f98aac4c1590577dd02b76d87eed74e836be03ee73fa1249787c
title: Add and Subtract Time
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dataweave-cookbook-add-and-subtract-time.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-add-and-subtract-time
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 7208f2b4f07518bee2845d95f4260cccb191fd12fcab9f403ec74406895e9f45
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
keywords: studio, anypoint, transform, transformer, format, xml, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Add and Subtract Time

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

This DataWeave example shows multiple addition and subtraction operations that deal with date and
time types.

The examples 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 Script:

```dataweave
%dw 2.0
output application/json
var numberOfDays = 3
---
{
  oneDayBefore: |2019-10-01T23:57:59Z| - |P1D|,
  threeDaysBefore: |2019-10-01T23:57:59Z| - ("P$(numberOfDays)D" as Period),
  a: |2019-10-01| - |P1Y|,
  b: |P1Y| - |2019-10-01|,
  c: |2019-10-01T23:57:59Z| - |P1Y|,
  d: |2019-10-01T23:57:59Z| + |P1Y|,
  e: |2019-10-01T23:57:59| - |P1Y|,
  f: |PT9M| - |23:59:56|,
  g: |23:59:56| + |PT9M|,
  h: |23:59:56-03:00| - |PT9M|,
  u: |23:59:56-03:00| - |22:59:56-03:00|,
  j: |23:59:56-03:00| - |22:59:56-00:00|,
  k: |2019-10-01T23:57:59| - |P2Y9M1D| - |PT57M59S| + |PT2H|,
  l: |23:59:56| - |22:59:56|,
  o: |2019-10-01| - |2018-09-23|,
  p: |2019-10-01T23:57:59Z| - |2018-10-01T23:57:59Z|,
  q: |2019-10-01T23:57:59| - |2018-10-01T23:57:59|
}
```

Output JSON:

```json
{
  "oneDayBefore": "2019-09-30T23:57:59Z",
  "threeDaysBefore": "2019-09-28T23:57:59Z",
  "a": "2018-10-01",
  "b": "2018-10-01",
  "c": "2018-10-01T23:57:59Z",
  "d": "2020-10-01T23:57:59Z",
  "e": "2018-10-01T23:57:59",
  "f": "23:50:56",
  "g": "00:08:56",
  "h": "23:50:56-03:00",
  "u": "PT1H",
  "j": "PT4H",
  "k": "2017-01-01T01:00:00",
  "l": "PT1H",
  "o": "PT8952H",
  "p": "PT8760H",
  "q": "PT8760H"
}
```

Note that the behavior of `o: |2019-10-01| - |2018-09-23|` changed in Mule 4.3.0. DataWeave 2.3.0,
which is supported by Mule 4.3.0, returns `"o": "PT8952H"`. DataWeave 2.2.0, which is supported by
Mule 4.2.0, returns `"o": "P1Y8D"`. To restore the 2.2.0 behavior, you can set the
`-M-Dcom.mulesoft.dw.date_minus_back_compatibility=true`. However, the setting does not affect the
Preview window in Studio. For guidance with this setting in Studio and on-prem deployments, see
[System Properties](https://docs.mulesoft.com/mule-runtime/latest/mule-app-properties-system). For CloudHub deployments,
see [Manage Properties for Applications on CloudHub](https://docs.mulesoft.com/cloudhub/cloudhub-manage-props).

## See Also

- [Period](https://docs.mulesoft.com/dataweave/2.11/dataweave-types#dw_type_dates_period)

- [Parse Dates](https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-parse-dates)

- [DataWeave Types](https://docs.mulesoft.com/dataweave/2.11/dataweave-types)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.11/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook)
