Typescript

How to Add Days to Date in TypeScript

The addition of days to date is the basic arithmetic operation to get the next or the previous date. This task can easily be performed in TypeScript similar to other programming languages. As we all know, TypeScript is the superset of JavaScript that includes almost all of its functionalities, methods, and properties. TypeScript uses the JavaScript “getDate()”, and “setDate()” methods, and the “Date()” constructor to add a number of days in the current/specific date. The “Date()” constructor returns the current date and time, the “getDate()” method gives the date and time for the given month, and the “setDate()” method sets the day to the date object.

This post explains the approaches to adding days to date in TypeScript.

How to Add Days to Date in TypeScript?

This section carries step-by-step instructions to add a number of days to the current and specified date by using these methods.

Let’s move on to the first step.

Step 1: Check Prerequisites

First, check if TypeScript is installed in your project set-up or not. It can be performed by utilizing the following “version” command:

tsc -v

 

In the above command, the “-v” flag represents the “version”. Also, it can be implied that TypeScript is installed with version “5.1.3”.

Step 2: Add Days to Date (Current)

Now, navigate to the “.ts” file of the project and add the following lines of code in it:

let current_date = new Date();
console.log('Current date: ',current_date);

current_date.setDate(current_date.getDate() + 4);
console.log('New date: ',current_date);

 
In the above code lines:

    • Create a date object “current_date” via the “new” keyword and the “Date()” constructor that returns the current date and time of the system.
    • Next, the “console.log()” method displays the “current_date” object output alongside the given statement.
    • After that, the “current_date” object is associated with the “setDate()” method having the “getDate()” method to add the specified number of days i.e., “4” in the given date.
    • Lastly, print the new date in the web console using the “console.log()” method.

Step 3: Compile and Run the “.ts” File

Once all is done, run the “tsc” compiler to compile the “.ts” file:

tsc

 

The compilation has been done successfully. It will now create the “main.js” file automatically.

Now, run the compiled “main.js” in this way and check the output:

node main.js

 

As seen in the output, the specified number of days is added to the current date of the system.

Pro Tip: Add Days to a Specific Date

In case the user wants to add the number of days on a particular date, then specify that date as a parameter of the “Date()” constructor in double quotes like this:

let current_date = new Date("2022-06-14");
console.log('Current date: ',current_date);

current_date.setDate(current_date.getDate() + 4);
console.log('New date: ',current_date);

 
Output

Now, run the compiled “.js” file again after the compilation:


Here, the given number of days is also added to the specified date.

Conclusion

In TypeScript, to add a specified number of days to date, use the “Date()” constructor, “setDate()” and the “getDate()” methods. These methods allow the users to add days both in the current date of the system and in the date specified by the user. Once these methods are applied properly, compile the “.ts” file and then run the automatically created “.js” file. This post deeply explained the approaches to adding days to date in TypeScript.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.