As a hobbyist coder, I'm facing a challenging problem with building a dynamic HTML/CSS calendar. My goal is to have cells filled in based on today's date. However, when I attempt to add days to fill in another 13 days by looping through HTML elements.innerHTML, I hit a roadblock.
If I use setDate(30 + 2) and then getDate(), everything works perfectly fine. Javascript recognizes that June ends on the 30th day, resulting in 2 (July 2nd), just as I wanted.
Here lies the issue - this method only functions properly if there is a single call. When I introduce a loop or make multiple calls to this code, the outcome varies. Could it be some asynchronous operation causing this inconsistency? Below is the code snippet:
const theDate = new Date();
const todaysDate = 30;
theDate.setDate(todaysDate + 1);
let result1 = theDate.getDate();
theDate.setDate(todaysDate + 2);
let result2 = theDate.getDate();
theDate.setDate(todaysDate + 3);
let result3 = theDate.getDate();
theDate.setDate(todaysDate + 4);
let result4 = theDate.getDate();
console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);