Typescript add and subtract days, months, years to Date | extension examples

This tutorial elucidates how to add days, months, and years to the current date in TypeScript, accompanied by examples.

TypeScript Date Manipulation: Adding Days, Months, Years

To create a function that takes a date and a number of days, and then adds those days to the date object, returning the updated Date object, follow this approach.

function addDays(date: Date, days: number): Date {
  date.setDate(date.getDate() + days);
  return date;
}
console.log(new Date());
console.log(addDays(new Date(), 2));

Output:

"2022-10-19T03:44:33.834Z"
"2022-10-21T03:44:33.839Z" `

To add 2 months, pass 60 days to the addDays function.

console.log(new Date());
console.log(addDays(new Date(),60));

Output:

"2022-10-19T03:45:41.756Z"
"2022-12-18T03:45:41.759Z"

Adding 1 year gives the next year’s date; pass 366 days to achieve this.

console.log(new Date());
console.log(addDays(new Date(),366));

Output:

"2022-10-19T03:46:37.901Z"
"2023-10-20T03:46:37.906Z"

TypeScript Date Extension for Obtaining Weekname

Suppose you want to get today’s weekname for a given date.

Follow these steps:

  • Create an Interface with an abstract method - getWeekName that returns a string name such as Sunday.
  • Add the prototype implementation for the getWeekName to the Date class.
  • Inside the method, construct an array of week names.
  • Date.getDays returns the day of the current week.
  • Finally, return the weekname from a given day.
interface Date {
  getWeekName: () => string;
}

Date.prototype.getWeekName = function () {
  var weekNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  return weekNames[this.getDay()];
};
console.log(new Date().getWeekName());

You can also extend it to add days.

TypeScript Extension: Adding Days to Date Class

interface Date {
  addDays: (days: number) => Date;
}

Date.prototype.addDays = function (days: number) {
  let valueDate = this.valueOf();
  valueDate += 86400000 * days;
  return new Date(valueDate);
};

console.log(new Date().addDays(1));

TypeScript Date Extension: Subtraction of Days, Months, Years

This example illustrates how to subtract days, months, and years from a given date.

interface Date {
  subtractDays: (days: number) => Date;
}

Date.prototype.subtractDays = function (days: number) {
  let valueDate = this.valueOf();
  valueDate -= 86400000 * days;
  return new Date(valueDate);
};

console.log(new Date().subtractDays(1));

Output:

"2022-10-30T03:58:35.947Z";

Typescript Subtract add days, months, year

Create a function subtractDays that takes the date and number of days and subtracts the days to date object and returns the Date object.

Here is an example to subtract 2 days from a given current date.

function subtractDays(date: Date, days: number): Date {
  var valueDate = date.valueOf();
  valueDate -= 86400000 * days;
  return new Date(valueDate);
}
console.log(new Date());
console.log(substractDays(new Date(), 2));

Output:

"2022-10-19T03:44:33.834Z"
"2022-10-21T03:44:33.839Z" `

To subtract 2 months from a current date, pass 60 days to the addDays function.

console.log(new Date());
console.log(subtractDays(new Date(), 60));

Output:

"2022-10-19T03:45:41.756Z"
"2022-08-18T03:45:41.759Z"

Subtract 1 year gives the previous year’s date, ie pass 366 days to the function

console.log(new Date());
console.log(substractDays(new Date(), 366));

Output:

"2022-10-19T03:46:37.901Z"
"20231-10-20T03:46:37.906Z"