0

I am trying to implement previous/next two weeks to my scheduler, but it is not working correctly.. This is what I try:

this.datesArray = [];
    const currentDate: Date = new Date(this.startDate);
    const lastDate: Date = new Date(this.endDate);
    currentDate.setDate(currentDate.getDate() - 14);
    lastDate.setDate(currentDate.getDate() + 30);

    while (currentDate < lastDate) {
      // var newDate: Date = new Date(currentDate);
      this.datesArray.push(currentDate.toDateString());
      currentDate.setDate(currentDate.getDate() + 1);
    }

Every click I trigger this code, but there is the following issue. First 2 times it works great, but 3rd time I click next, last date.SetDate bugs out, it addds 60 days instead of 30. Here is what I mean for the previous week:

previous 2 weeks example

It shows current date Feb 22, but then adds 30 days, and it shows Apr 21 somehow..

Anyone had similar issues?

Nemanja Grabovac
  • 858
  • 2
  • 15
  • 30
  • @Rajesh don't think that is an issue. Look in the image above, lastDate is set incorrectly, it shouldn't be Apr 21, should me March...It added more than 30 days – Nemanja Grabovac Mar 08 '18 at 13:51
  • What is the value of `this.endDate` ? – Titus Mar 08 '18 at 13:58
  • @Titus when component loads, it is the current date (8th of March atm), later on it changes to current + 30 days – Nemanja Grabovac Mar 08 '18 at 14:07
  • 1
    `setDate` changes the day of the month, in case of overflow or underflow the month will be changed as well. You're using `lastDate.setDate(currentDate.getDate() + 30)` which means that you're adding some days (more then a month) to a date variable that is already in month `Mar`. – Titus Mar 08 '18 at 14:14
  • @Titus How could I do this then, do you have a solution? – Nemanja Grabovac Mar 08 '18 at 14:21
  • 1
    That depends on what you're trying to do. Do you want to add `30` days to the date stored in the `currentDate` variable ? Do you want to add these `30` days before you subtract `14` or after that ? – Titus Mar 08 '18 at 14:22
  • @Titus I want to add 30 days after i substract 14 days, or add 14 days to current date (depending if i want next or previous 2 weeks) – Nemanja Grabovac Mar 08 '18 at 14:25
  • try: `lastDate = new Date(currentDate.getTime()).setDate(currentDate.getDate() + 30)` or something like that. – Titus Mar 08 '18 at 14:27
  • Possible duplicate of [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – thmsdnnr Mar 08 '18 at 14:42
  • @thmsdnnr not really, i posted the problem I get while using that solution (maybe in a wrong way), read the question before commenting please. – Nemanja Grabovac Mar 08 '18 at 14:44
  • @NemanjaGrabovac — Hi Nemanja, I read your question (and began responding). That's how I came across the other thread. See particularly [post #4](https://stackoverflow.com/a/19691491/7316502). I believe that the bug you're encountering is that your method sometimes gives the correct result, but not when the resulting date is in a different month or year. – thmsdnnr Mar 08 '18 at 14:50
  • IMO, this would be easier with MomentJS. `moment(this.startDate).add(30, 'days');` –  Mar 08 '18 at 14:58

0 Answers0