This is just regular JavaScript, no need for TypeScript.
CopyyourDate = new Date(yourDate.getTime() + (1000 * 60 * 60 * 24));
1000 milliseconds in a second * 60 seconds in a minute * 60 minutes in an hour * 24 hours.
Additionally you could increment the date:
CopyyourDate.setDate(yourDate.getDate() + 1);
The neat thing about setDate is that if your date is out-of-range for the month, it will still correctly update the date (January 32 -> February 1).
See more documentation on setDate on MDN.
Answer from Adam on Stack OverflowThis is just regular JavaScript, no need for TypeScript.
CopyyourDate = new Date(yourDate.getTime() + (1000 * 60 * 60 * 24));
1000 milliseconds in a second * 60 seconds in a minute * 60 minutes in an hour * 24 hours.
Additionally you could increment the date:
CopyyourDate.setDate(yourDate.getDate() + 1);
The neat thing about setDate is that if your date is out-of-range for the month, it will still correctly update the date (January 32 -> February 1).
See more documentation on setDate on MDN.
Copy addDays(date: Date, days: number): Date {
date.setDate(date.getDate() + days);
return date;
}
In your case days = 1
Handle dates
Does anyone have a good helper function to handle dates in JavaScript/typescript?
For example if the date is 2020-09-27 and you want to add 7 days to the date it updates correctly. Should be 2020-09-04 after adding 7 days .
Thank you for the help
For example, consider the following implementation:
Date.prototype.isBefore = function(date: Date): boolean {
return this.getTime() < date.getTime();
};With this, you can compare dates using the following interface:
const date1 = new Date('2021-01-01');
const date2 = new Date('2021-01-02');
console.log(date1.isBefore(date2)); // trueIs there any problem with such an extension?
There are several libraries that make it easier to handle dates in JavaScript/TypeScript, but major ones seem to avoid such extensions. (Examples: day.js, date-fns, luxon)
Personally, I think it would be good to have a library that adds convenient methods to the standard Date, like ActiveSupport in Ruby on Rails. If there isn't one, I think it might be good to create one myself. Is there any problem with this?
Added on 2024/11/12:
Thank you for all the comments.
It has been pointed out that such extensions should be avoided because they can cause significant problems if the added methods conflict with future standard libraries (there have been such problems in the past).