How to add one day to a date object in TypeScript

How to add one day to a date object in TypeScript:

We can create a date object by using the Date() constructor in TypeScript. If we don’t pass anything to the constructor, it will create one date object with the current date-time info i.e. it will hold the current date and time.

The date object has different getter and setter methods. We can read different properties of a date object and we can also edit these properties.

To add one day to a date object, we have to read the date defined by the object, add one day to it and set it to the same object itself.

Following are the methods we will use to add one day to a date object:

getDate():

The getDate() method returns the date of the month for the given date.

It is defined as like below:

getDate()

Return value of getDate():

The getDate() method returns the day number of the month for a given date object. The returned value is in the range of 1 to 31.

setDate(day):

The setDate() method is used to set the day to a date object. This method takes a number in the range of 1 to 31 and assigns that value to a date object.

It is defined as like below:

setDate(day)

Here, day is a number to represent the number of days to set to the date object.

Return value of setDate:

The setDate method returns the number of milliseconds from 1 January 1970 00:00:00 UTC. We will not use this value in this example as it also modifies the date object.

Out of range values:

We can pass values other than 1 to 31. It updates the date object automatically. For example, if we pass 0, it will set the date object to the last day of the previous month. Similarly, for a bigger value, it will change the date object to the next month.

It also accepts negative values. For negative values, it counts backward from the last day of the previous month. For example, -2 will result two days before the last day of the previous month.

Example 1: Add one day to a date object in TypeScript:

To add one day to a date object in TypeScript, we have to use both getDate and setDate methods. We will use getDate to get the date and setDate to set the date by adding 1 to the return value of getDate.

Below is the complete program:

let given_date = new Date("2022-06-12");
console.log('Given date: ',given_date);

given_date.setDate(given_date.getDate() + 1);
console.log('New date: ',given_date);

It will print the below output:

Given date:  2022-06-12T00:00:00.000Z
New date:  2022-06-13T00:00:00.000Z

As you can see here, one day is added to the date object. The date was created as 12th June and it became 13th June.

Example 2: Add one day to the last day of a month in TypeScript:

The previous example added one day to a date in the middle of the month. Let’s change the program to add one day to a month-end date. It will automatically move to the first of next month.

let given_date = new Date("2022-06-30");
console.log('Given date: ',given_date);

given_date.setDate(given_date.getDate() + 1);
console.log('New date: ',given_date);

For this example, the original date is 30th June and it added one day to this date object. It will change the date to first of next month i.e. 1st July.

Given date:  2022-06-30T00:00:00.000Z
New date:  2022-07-01T00:00:00.000Z

Example 3: Add one day to 31st December:

If we add one day to a date object that is for 31st December of some year, it will change the date to 1st January of the next year.

let given_date = new Date("2022-12-31");
console.log('Given date: ',given_date);

given_date.setDate(given_date.getDate() + 1);
console.log('New date: ',given_date);

It will print:

Given date:  2022-12-31T00:00:00.000Z
New date:  2023-01-01T00:00:00.000Z

i.e. 31st December, 2022 is changed to 1st Jan, 2023 if we add a day to it.

Example 4: Add one day to the current date-time:

If we don’t pass any value to the Date constructor, it will create a date object with the current date-time value. If we add one day to this object, it will point to tomorrow’s date-time.

let given_date = new Date();
console.log('Given date: ',given_date);

given_date.setDate(given_date.getDate() + 1);
console.log('New date: ',given_date);

It will increment the date by one. But, the time won’t be changed.

Given date:  2022-07-15T14:40:15.284Z
New date:  2022-07-16T14:40:15.284Z

You might also like: