I have always contended that date math is one of the hardest and simultaneously easiest tasks programmer perform. It's difficult because as developers, the calendar isn't exactly our friend. The number of days in the month are not fixed and even the lenth of the year in days is a floating point number. But it's easy because we get to stand on the shoulders of giants.
But adding days is just the tip of the iceberg. If you're open to using a third-party library, you absolutly MUST check out moment.js. This library takes all the hassle out of dealing with dates. Not just adding them, but formatting them, and doing more complicated date manipulations.
Here are a few examples of relative time calculations:
Our original problem can be solved easily with Moment JS. Here are a few examples:
moment().subtract(10, 'days').calendar(); // 07/31/2020 moment().calendar(); // Today at 3:43 PM moment().add(1, 'days').calendar(); // Tomorrow at 3:43 PM moment().add(3, 'days').calendar(); // Thursday at 3:43 PM moment().add(10, 'days').calendar();
Every language needs easy to use date manipulation libraries and we have them both built into the language, and more extensively in third party libraries.