Reputation: 714
I am trying to add 12 days to a specific date. but I am getting the new date as not expected. Here is my code
addDays(days: number): any {
let startDate = new Date(this.selectedDeliveryDate);
let endDate = new Date();
endDate = new Date(startDate.setDate(startDate.getDate() + days))
console.log(startDate, endDate, days)
}
this.selectedDeliveryDate = Wed Dec 08 2021 05:30:00 GMT+0530 (India Standard Time)
and
days = 12
I am getting the endDate
as Tue Feb 20 2024 17:49:13 GMT+0530 (India Standard Time)
EDIT I have printed the following code to test
console.log((startDate.getDate() + days));
and selected 07-12-2021
as start date
and days
is 12
. I am getting the result like 712
. Its just appending the number of days to the day.
Upvotes: 0
Views: 2941
Reputation: 714
I fixed the issue. The issue is my days
parameter is not integer, so the +
operator just concatenating the two values. So before adding days, I just converted days to integer.
addDays(days) {
days = parseInt(days);
let startDate = new Date(this.selectedDeliveryDate);
let endDate = new Date();
this.freeSlotEndDate = endDate.setDate(startDate.getDate() + days)
}
Upvotes: 1
Reputation: 2397
I tested it on chrome and firefox and it works correctly for me. What browser are you using?
chrome:
firefox:
Upvotes: 0
Reputation: 367
Try this on your imports:
import * as moment from "moment";
Then, your funtion:
addDays(days: number): any {
let startDate = new Date(this.selectedDeliveryDate);
let endDate = new Date();
endDate = moment(startDate).add(days, "days");
console.log(startDate, endDate, days)
}
Upvotes: 0