Currently, I find myself a bit puzzled as to why my code is not functioning as expected, and I am hopeful that you all could assist me in solving this issue.
The data structure I am working with consists of years and corresponding months.
chosenMonths = {[
2022: [0,1,2]
2018: [9,10]
2017: [11]
]}
I have a function designed to convert numerical values into their respective month names:
const writeOutMonth = function(num, locale) {
const date = new Date();
date.setMonth(num);
return date.toLocaleString(locale, {month: 'long',});
}
Within my code, there is an Object.keys method to display the year followed by the written out months on the screen.
${Object.keys(chosenMonths).reverse().map((year) => html`<p1> ${year}: ${writeOutMonth(chosenMonth[year].sort().join(', '), 'en-gb')}</p>`)}
At present, the output appears as follows:
2022: Invalid Date
2021: Invalid Date
2017: December
My query is: why does it show "Invalid Date" for some entries even though multiple numbers are specified for certain years, and how can I rectify this?