@spa-tools/utilities
addYears()
The addYears()
function adds a number of years to a given date object,
timestamp integer, or date string.
Usage
import { addYears } from '@spa-tools/utilities';
const date = new Date('2021-01-01');
const newDate1 = addYears(date, 5);
console.log(newDate1.toLocaleDateString('en-US'));
// --> '01/01/2026'
const newDate2 = addYears(date.getTime(), 6);
console.log(newDate2.toLocaleDateString('en-US'));
// --> '01/01/2027'
const newDate3 = addYears('2021-01-01', 7);
console.log(newDate3.toLocaleDateString('en-US'));
// --> '01/01/2028'
Arguments
Name | Type | Required? | Description |
---|---|---|---|
date | string | number | Date | yes | The date value to add years to as either date string, timestamp integer, or date object |
years | number | yes | The number of years to add |
Returns
Date
- The new date object with the added years.