Question: I'm trying to set a date with 4 days range (From: today - 2, To: today + 2).
Today, on 31. august I found the bug in my code, which says `Invalid date`.
I'm changing the date like this, and If I `console.log()` it, it says `-1`.

Any help would be appreciated.

```
date: Date = new Date();
defaultDay: string = ("0" + (this.date.getDate() - 2)).slice(-2)

```

P.S: I checked the moment.JS library, but currently I don't have that much time to change the whole project to it.

Answer: 
You're doing the basic arithmetic. Subtracting 2 from 1 will result in -1. You may try to set the date first and then get the day from the date object.

The reason is that [`setDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate) sets the day relatively when the argument is not in the range (less than or equal to 0).

```js
let start = new Date();
let end = new Date();
start.setDate(start.getDate() - 2);
end.setDate(end.getDate() + 2);
console.log(start.getDate(), end.getDate());
```
You can always do in a way @31piy has suggested. But Date handling is lot easier if you do it using [MomentJS](https://momentjs.com/docs/).

You can do it easily like

```
const today = moment()
const two_days_after_today = moment().add(2, 'day')
const two_days_before_today = moment().subtract(2, 'day')

```