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).
Extending prototype in javascript may not be a good idea, especially in professional codebases.
What you want to do is extend the native Date class:
class MyCustomDate extends Date {
addDays(days) {
const date = new MyCustomDate(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
}
const today = new MyCustomDate();
const nextWeek = today.addDays(7)
console.log(nextWeek)
This way, if one day Javascript implements a native addDays method, you won't break anything.
I use something like:
new Date(dateObject.getTime() + amountOfDays * 24 * 60 * 60 * 1000)
Works with day saving time:
new Date(new Date(2014, 2, 29, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
Works with new year:
new Date(new Date(2014, 11, 31, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
It can be parametrized:
function DateAdd(source, amount, step) {
var factor = 1;
if (step == "day") factor = 24 * 60 * 60 * 1000;
else if (step == "hour") factor = 60 * 60 * 1000;
...
new Date(source.getTime() + amount * factor);
}