Subtraction operation
Learn how to subtract a value from another.
The subtraction operation calculates the difference between two numeric or temporal operands and returns the difference.
To use the subtraction operation, place a - sign between two numeric values:
/*<operand>*/ - /*<operand>*/
Alternatively, for a more natural language feel, you can write it as follows:
/*<operand>*/ minus /*<operand>*/
Here is a practical example:
5 - 3 // 2
For temporal values, the subtraction operation allows you to perform calculations involving dates, times, and durations:
today - 1 day
The above example subtracts one day from the current date, resulting in the date of yesterday. You can chain multiple operations together to perform more complex calculations:
today - 1 month - 1 week
In contrast to addition, subtraction is not commutative. This means that you cannot subtract a date from a period of time, but you can subtract a period of time from a date. For example, today - 1 day works, but not 1 day - today.
Finally, note that the subtraction operation does not perform any automatic conversions for temporal values. Be sure to use the cast modifier to convert a value to a date or time before subtracting it from another value, as shown below:
"2021-01-01" as date - 1 day