SUBTRACT_FROM_DATE
The function SUBTRACT_FROM_DATE subtracts a given number of years, months, weeks, or days from a given Date.
This function takes three pieces of input: a Date, a Number, and a string. The string designates which value in the given Date will be modified: "year", "month", "week", or "day". This function subtracts the given Number from the designated value, and returns the resulting Date. Calculation takes into account irregularities such as daylight savings time and leap years.
Declaration
Parameters
date
(required, type: Date
)
Any valid Date.
number
(required, type: Number
)
The number of units to be subtracted from the given Date. This number can be negative; if given a negative number, SUBTRACT_FROM_DATE will subtract the negative number from the specified value in the Date, ultimately increasing the original value.
unit_string
(required, type: string
)
The units in the given Date that will be modified: "year", "month", "week", or"day". In order to be correctly parsed by SUBTRACT_FROM_DATE, the given string must exactly match one of the relevant values given by this table.
Return Values
modified_date
(type: Date
)
The Date that results from subtracting given number of units from the given Date. Calculation takes into account irregularities such as daylight savings time and leap years.
Examples
Assume all examples have access to the following Date:
The following example takes the date described in example_date
and subtracts 3 days from it. Not that the returned Date is identical to example_date
, save for the day value:
The following example takes the date described in example_date
and subtracts 6 months from it. Note how this results in a change not only in the month value of the resulting Date, but also in the the year:
The following example takes the date and time described in example_date
and subtracts -1 day from it. Note how this effectively adds 1 to the given day value:
If SUBTRACT_FROM_DATE is given a unit_string
it cannot parse, it simply returns the given Date unmodified, such as in the following example:
Discussion
Subtracting negative values is the same as adding positive values, allowing SUBTRACT_FROM_DATE to mimic the functionality of ADD_TO_DATE.
Last updated