|
| 1 | += Temporal operators |
| 2 | +:description: Information about Cypher's temporal operators. |
| 3 | +:table-caption!: |
| 4 | + |
| 5 | +Cypher contains the following temporal operators: |
| 6 | + |
| 7 | +* Adding a xref::values-and-types/temporal.adoc#cypher-temporal-durations[`DURATION`] to either a xref::values-and-types/temporal.adoc#cypher-temporal-instants[temporal instant value] or another `DURATION`: `+` |
| 8 | +* Subtracting a `DURATION` from either a temporal instant value or another `DURATION`: `-` |
| 9 | +* Multiplying a `DURATION` with a xref::values-and-types/property-structural-constructed.adoc#property-types[number (`INTEGER` or `FLOAT`)]: `*` |
| 10 | +* Dividing a `DURATION` by a number: `/` |
| 11 | +
|
| 12 | +For additional expressions that evaluate to a xref:values-and-types/temporal.adoc[temporal value type], see: |
| 13 | + |
| 14 | +* xref:functions/temporal/index.adoc[Temporal functions -- instant types] |
| 15 | +* xref:functions/temporal/duration.adoc[Temporal functions -- duration types] |
| 16 | +
|
| 17 | +.Value returned from temporal operators |
| 18 | +[options="header"] |
| 19 | +|=== |
| 20 | +| Operator | Left-hand operand | Right-hand operand | Result value type |
| 21 | + |
| 22 | +| `+` |
| 23 | +| Temporal instant |
| 24 | +| `DURATION` |
| 25 | +| The type of the temporal instant |
| 26 | + |
| 27 | +| `+` |
| 28 | +| `DURATION` |
| 29 | +| Temporal instant |
| 30 | +| The type of the temporal instant |
| 31 | + |
| 32 | +| `-` |
| 33 | +| Temporal instant |
| 34 | +| `DURATION` |
| 35 | +| The type of the temporal instant |
| 36 | + |
| 37 | +| `+` |
| 38 | +| `DURATION` |
| 39 | +| `DURATION` |
| 40 | +| `DURATION` |
| 41 | + |
| 42 | +| `-` |
| 43 | +| `DURATION` |
| 44 | +| `DURATION` |
| 45 | +| `DURATION` |
| 46 | + |
| 47 | +| `*` |
| 48 | +| `DURATION` |
| 49 | +| Number |
| 50 | +| `DURATION` |
| 51 | + |
| 52 | +| `*` |
| 53 | +| Number |
| 54 | +| `DURATION` |
| 55 | +| `DURATION` |
| 56 | + |
| 57 | +| `/` |
| 58 | +| `DURATION` |
| 59 | +| Number |
| 60 | +| `DURATION` |
| 61 | + |
| 62 | +|=== |
| 63 | + |
| 64 | +[[adding-subtracting-durations]] |
| 65 | +== Adding and subtracting `DURATION` values |
| 66 | + |
| 67 | +`DURATION` values can be added and subtracted from temporal instant values, such as `LOCAL DATETIME`. |
| 68 | +In the below example, the xref:functions/temporal/index.adoc#functions-localdatetime[`localdatetime()`] function is used to create a `LOCAL DATETIME` value, and the xref:functions/temporal/duration.adoc#functions-durations[`duration()`] function is used to create a `DURATION` value. |
| 69 | + |
| 70 | +.Add and subtract a `DURATION` value to/from a `LOCAL DATETIME` |
| 71 | +[source, cypher] |
| 72 | +---- |
| 73 | +WITH localdatetime({year:1984, month:10, day:11, hour:12, minute:31, second:14}) AS aDateTime, |
| 74 | + duration({years: 12, nanoseconds: 2}) AS aDuration |
| 75 | +RETURN aDateTime + aDuration AS addition, |
| 76 | + aDateTime - aDuration AS subtraction |
| 77 | +---- |
| 78 | + |
| 79 | +.Result |
| 80 | +[role="queryresult",options="header,footer",cols="2*<m"] |
| 81 | +|=== |
| 82 | +| addition | subtraction |
| 83 | + |
| 84 | +| 1996-10-11T12:31:14.000000002 | 1972-10-11T12:31:13.999999998 |
| 85 | + |
| 86 | +2+d|Rows: 1 |
| 87 | +|=== |
| 88 | + |
| 89 | +When adding or subtracting a `DURATION` that results in a non-existing date, Cypher truncates the date to the nearest valid date. |
| 90 | +For example, if adding 1 month to January 31st, the result will not be February 31st (an invalid date) but February 28th (or 29th in a leap year). |
| 91 | + |
| 92 | +.Add 1 month to January 31st |
| 93 | +[source, cypher] |
| 94 | +---- |
| 95 | +RETURN date("2011-01-31") + duration("P1M") AS truncatedDate |
| 96 | +---- |
| 97 | + |
| 98 | +.Result |
| 99 | +[role="queryresult",options="header,footer",cols="1*<m"] |
| 100 | +|=== |
| 101 | +| truncatedDate |
| 102 | + |
| 103 | +| 2011-02-28 |
| 104 | + |
| 105 | +1+d|Rows: 1 |
| 106 | +|=== |
| 107 | + |
| 108 | +When adding two `DURATION` values to a temporal instant value, the order in which the durations are applied affects the result. |
| 109 | + |
| 110 | +.Add two `DURATION` values to a `DATE` |
| 111 | +[source, cypher] |
| 112 | +---- |
| 113 | +RETURN (date("2011-01-31") + duration("P1M")) + duration("P12M") AS date1, |
| 114 | + date("2011-01-31") + (duration("P1M") + duration("P12M")) AS date2 |
| 115 | +---- |
| 116 | + |
| 117 | +In `date1`, durations are added one after the other, so the date is truncated after adding the first month, resulting in `2012-02-28`. |
| 118 | +In `date2`, the durations are combined first, and truncation only happens once, resulting in `2012-02-29`. |
| 119 | + |
| 120 | +.Result |
| 121 | +[role="queryresult",options="header,footer",cols="2*<m"] |
| 122 | +|=== |
| 123 | +| date1 | date2 |
| 124 | + |
| 125 | +| 2012-02-28 | 2012-02-29 |
| 126 | + |
| 127 | +2+d|Rows: 1 |
| 128 | +|=== |
| 129 | + |
| 130 | +[[ignored-components]] |
| 131 | +=== Ignored components |
| 132 | + |
| 133 | +When adding or subtracting a `DURATION` value to a temporal instant value, any xref::values-and-types/temporal.adoc#cypher-temporal-duration-component[`DURATION` components] that do not apply to that specific type are ignored. |
| 134 | +(For information about what components are supported by temporal instant values, see xref::values-and-types/temporal.adoc#cypher-temporal-accessing-components-temporal-instants[Components of temporal instants]). |
| 135 | +For example, when adding a `DURATION` to a `DATE`, only the `year`, `month`, and `day` components of a `DURATION` value are considered, while `hour`, `minute`, `second`, and `nanosecond` are ignored. |
| 136 | +This behavior also applies to `LOCAL TIME` and `ZONED TIME`. |
| 137 | + |
| 138 | +.Add and subtract a `DURATION` to/from a `DATE` |
| 139 | +[source, cypher] |
| 140 | +---- |
| 141 | +WITH date({year:1984, month:10, day:11}) AS aDate, |
| 142 | + duration({years: 12, nanoseconds: 2}) AS aDuration |
| 143 | +RETURN aDate + aDuration AS addition, |
| 144 | + aDate - aDuration AS subtraction |
| 145 | +---- |
| 146 | + |
| 147 | +.Result |
| 148 | +[role="queryresult",options="header,footer",cols="2*<m"] |
| 149 | +|=== |
| 150 | +| addition | subtraction |
| 151 | + |
| 152 | +| 1996-10-11 | 1972-10-11 |
| 153 | + |
| 154 | +2+d|Rows: 1 |
| 155 | +|=== |
| 156 | + |
| 157 | +== Multiplying and dividing `DURATION` values |
| 158 | + |
| 159 | +When multiplying or dividing a `DURATION`, each component is handled separately. |
| 160 | +In multiplication, the value of each component is multiplied by the given factor, while in division, each component is divided by the given number. |
| 161 | +If the result of the division does not fit into the original components, it overflows into smaller components (e.g. converting days into hours). |
| 162 | +This overflow also occurs when multiplying with fractions. |
| 163 | + |
| 164 | +.Multiply and divide a `DURATION` value |
| 165 | +[source, cypher] |
| 166 | +---- |
| 167 | +WITH duration({days: 14, minutes: 12, seconds: 70, nanoseconds: 1}) AS aDuration |
| 168 | +RETURN aDuration, |
| 169 | + aDuration * 2 AS multipliedDuration, |
| 170 | + aDuration / 3 AS dividedDuration |
| 171 | +---- |
| 172 | + |
| 173 | +.Result |
| 174 | +[role="queryresult",options="header,footer",cols="3*<m"] |
| 175 | +|=== |
| 176 | +| aDuration | multipliedDuration | dividedDuration |
| 177 | +| P14DT13M10.000000001S | P28DT26M20.000000002S | P4DT16H4M23.333333333S |
| 178 | + |
| 179 | +3+d|Rows: 1 |
| 180 | +|=== |
| 181 | + |
0 commit comments