so I want to get the current month and the next 12 months in vanilla js. For example, upon writing this question it's April 2022, So I want to have this output:
April 2022 May 2022 June 2022 July 2022 August 2022 September 2022 October 2022 November 2022 December 2022 January 2023 February 2023 March 2023 April 2023
CodePudding user response:
let months=['January','February','March','April','May','June','July','August','September','October','November','December']
let d = new Date();
let year = d.getFullYear();
let month = d.getMonth();
let next12Months = [];
for(let i = 0; i < 13; i ){
if(month i == 12)year
let item = months[(month i)] ' ' year
next12Months.push(item)
}
console.log(next12Months)
CodePudding user response:
for (let i = 0; i < 13; i ) {
let month = new Date().getMonth() 1 i;
if (month > 12) {
month = month - 12;
}
const nameMonth = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec",
};
console.log(
new Date().getFullYear() " Year " nameMonth[month] " Month "
);
}