|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Date and Time Calculation > DTO. - Date Time Operations > Date & Time Calculations |
MiniRobotLanguage (MRL)
DTO.SubtractYears Command
Subtract Years from a Date
Intention
The DTO.SubtractYears command (alias subyr) subtracts a specified number of years from a given date. It is particularly useful for calculating historical dates, ages, or contract start dates based on an end date.
The command correctly accounts for leap years. For example, subtracting 1 year from February 29, 2024, will correctly result in February 28, 2023.
Syntax
DTO.SubtractYears|date|years|[format]|[$$RES]
Parameter Explanation
date - The source date string (e.g., "29.02.2024") or a variable containing one.
years - The integer number of years to subtract from the date.
format - (Optional) The format mask for the input `date` and the resulting output date.
$$RES - (Optional) The variable to store the resulting date. If omitted, the source variable provided in `date` is updated.
Example
'**********************************************
' DTO.SubtractYears - Sample
'**********************************************
'
' Set the starting date to a leap day.
$$DAA = "29.02.2024"
' Subtract 1 year. The target year (2023) is not a leap year.
DTO.SubtractYears|$$DAA|1||$$RES
' Display the result. The date will be adjusted to the last day of Feb.
MBX.Original: $$DAA | Result: $$RES
' Expected output: Original: 29.02.2024 | Result: 28.02.2023
ENR.
Remarks
The command is leap-year aware. If you subtract years from February 29th and the resulting year is not a leap year, the date will be automatically adjusted to February 28th.
See also: