0

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.

geeth
  • 704
  • 2
  • 14
  • 42

3 Answers3

1

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)
}
geeth
  • 704
  • 2
  • 14
  • 42
0

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)
}
Kibé M.C
  • 243
  • 3
  • 8
0

I tested it on chrome and firefox and it works correctly for me. What browser are you using?

chrome:

enter image description here

firefox:

enter image description here

Chris
  • 2,117
  • 13
  • 18