I want to ask how to make a function javascript reactjs calculate the date and month of the year automatically with a distance of 10 days from 17-10-2020, after calculating from 17-10-2020 it will be like this 27-10-2020
Asked
Active
Viewed 68 times
0
-
1There are libraries for that such as: https://momentjs.com/. Do you have to implement it yourself? – Magofoco Sep 29 '20 at 16:40
-
You want a function that takes a date input and returns a date 10 days in the future? – TKoL Sep 29 '20 at 16:41
-
Yes, it is true – putra irawan Sep 29 '20 at 16:43
-
if You decide to use Moment.js the following would work `date.add(10, "days")` where `date` is a Moment.js object. – 3limin4t0r Sep 29 '20 at 17:04
1 Answers
2
This function should work:
const tenDaysInFuture = date => {
const newDate = new Date(date);
newDate.setDate(date.getDate() + 10);
return newDate;
}
console.log(tenDaysInFuture(new Date('2020-09-29')));
console.log(tenDaysInFuture(new Date('2020-09-09')));
console.log(tenDaysInFuture(new Date('2020-10-17')));

TKoL
- 13,158
- 3
- 39
- 73