Recently, I encountered an issue with a simple code snippet that seems to produce inconsistent results. Take a look at the function below:
addDays(date: Date, days: number): Date {
console.log('adding ' + days + ' days');
console.log(date);
date.setDate(date.getDate() + days);
console.log(date);
return date;
}
Let's see some examples of its output:
Everything seems fine at first:
adding 7 days
error-chart.component.ts:118 Tue Aug 02 2016 00:00:00 GMT+0200 (Vest-Europa (sommertid))
error-chart.component.ts:120 Tue Aug 09 2016 00:00:00 GMT+0200 (Vest-Europa (sommertid))
But then it starts to malfunction:
adding 14 days
error-chart.component.ts:118 Tue Aug 02 2016 00:00:00 GMT+0200 (Vest-Europa (sommertid))
error-chart.component.ts:120 Thu Mar 02 2017 00:00:00 GMT+0100 (Vest-Europa (normaltid))
Suddenly, it jumps ahead by two years:
adding 14 days
error-chart.component.ts:118 Thu Jul 07 2016 00:00:00 GMT+0200 (Vest-Europa (sommertid))
error-chart.component.ts:120 Thu Jun 14 2018 00:00:00 GMT+0200 (Vest-Europa (sommertid))
And then, it continues to exhibit unexpected behavior, now jumping four years:
adding 14 days
error-chart.component.ts:118 Thu Jun 14 2018 00:00:00 GMT+0200 (Vest-Europa (sommertid))
error-chart.component.ts:120 Thu Apr 14 2022 00:00:00 GMT+0200 (Vest-Europa (sommertid))