I am trying to generate a sequence of dates. However, when generating JS, it constantly misses October 29, 2023. Could someone explain what could be the reason?
In this post someone noted that on this day there is a transition between winter and summer time. Maybe this is the reason, but then how to correctly build sequences of dates?
Here is an sample of the code I use:
let data = {
"2024-03-01": 4,
"2024-03-02": 1,
"2024-03-03": 3,
"2024-03-04": 6,
"2024-03-05": 2,
"2024-03-06": 0,
"2024-03-07": 5,
"2024-03-08": 1,
"2024-03-09": 3
}
function adjustDataToHave365days(data) {
const dates = Object.keys(data).map(date => new Date(date))
const firstDate = dates[0]
let startDate = new Date(firstDate)
startDate.setDate(startDate.getDate() - (365 - dates.length))
console.log(firstDate)
console.log(startDate)
const result = {};
let currentDate = new Date(startDate);
while (currentDate <= firstDate) {
console.log(currentDate)
const formattedDate = currentDate.toISOString().split('T')[0];
result[formattedDate] = 0;
currentDate.setDate(currentDate.getDate() + 1);
}
Object.keys(data).forEach(date => {
result[date] = data[date];
});
return result;
}
console.log(adjustDataToHave365days(data))
Output:
...
2023-10-26: 0,
2023-10-27: 0,
2023-10-28: 0,
2023-10-30: 0,
2023-10-31: 0,
2023-11-01: 0,
...
It just skips 2023-10-29.
I tried different combinations of dates and different algorithms.
Dreamer DeLy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.