{

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


This tutorial explains how to add a day, month, and year to the current date in Typescript with examples

Typescript date add days, months, year

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

 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 next year’s date, ie pass 366 days

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 to get Weekname

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

  • 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 a method, Constructed an array of week names.
  • Date.getDays returns the day of a current week
  • Finally, Return the weekname from a given day.

interface Date
{
    getWeekName: () => String;
}

Date.prototype.getWeekName = function()
{
    var date = new Date();
        var weekNames = new Array(7);
        weekNames[0] = "Sunday";
        weekNames[1] = "Monday";
        weekNames[2] = "Tuesday";
        weekNames[3] = "Wednesday";
        weekNames[4] = "Thursday";
        weekNames[5] = "Friday";
        weekNames[6] = "Saturday";
    return          weekNames[date.getDay()];

};

console.log(new Date().getWeekName())

you can also add a days extension

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 subtracts days, months, years

This example subtracts days, months years from a given date

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

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


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


};
console.log(new Date().substractDays(1))

Output:

"2023-10-20T03:58:35.944Z"
"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" 
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.