I have been trying to get the dates in the format "Month Day" (eg Jun 28) for an app I'm making for the next 4 days. Here's the code I've been working on, based on this solution I found for the next day:
now.setDate(now.getDate() + 1)
which returns me an epoch timestamp.
let now = new Date();
function timeConverter(epoch) {
let a = new Date(epoch * 1000);
let months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
let month = months[a.getMonth()];
let date = a.getDate();
let time = month + " " + date;
return time;
}
let tomorrow = now.setDate(now.getDate() + 1);
let tomorrowDay = document.querySelector(".next1");
tomorrowDay.innerHTML = timeConverter(tomorrow);
let afterTomorrow = now.setDate(now.getDate() + 2);
let aftertomorrowDay = document.querySelector(".next2");
aftertomorrowDay.innerHTML = timeConverter(afterTomorrow);
And so on where I add +3 and +4 for the next 2 days. This gives me random dates, such as, respectively, Jul 19, Jan 9, Mar 27, and Mar 10. I don't know what is wrong with the code specifically, but there is a detail, which is the fact that when I console log today's date
let now = new Date();
console.log(now)
It tells me it's July 8th, when it's June 28th. I don't know if it's that that's messing everything up or if the code is wrong. Can someone help me with suggestions to improve the code or any ways to get the correct days in this format for the next 4 days?