I'm building a weekly picker. For the most part it works as intended, I actually get the right week when I click on it, but the selected days within the calendar portion of the picker are displaying a day behind. For example, in the screenshot below, the week picked is 2020-06-08 to 2020-06-14
yyyy-mm-dd format by the way. But as you can see in the picker, it displays the week shifted - 1
I'll post the relevant code but hard as I tried, I couldn't make a codesandbox for you. Way too many dependencies
Here is the convertDate function for the formatting. This is not related to the problem but in case you're wondering what was in it from the main function
const convertDate = (date) => {
let dt = new Date(date);
var yyyy = dt.getFullYear();
var mm = dt.getMonth() + 1; // getMonth() is zero-based
var dd = dt.getDate();
return `${yyyy}-${(mm > 9 ? '' : '0') + mm}-${(dd > 9 ? '' : '0') + dd}`;
};
And this the actual function that renders the days in the picker
EDIT (with help from Sergei
const handleClick = (e) => {
setDate(new Date(date.setDate(e.target.id)));
// added this to calculate the offset to my local timezone
let localDate = new Date(date.setDate(e.target.id));
var localoffset = new Date().getTimezoneOffset();
var timeZoneFromDB = -4.00;
var tzDifference = timeZoneFromDB * localoffset + localDate.getTimezoneOffset();
var offsetTime = new Date(localDate.getTime() + tzDifference * localoffset * 1000);
const firstDay = new Date(
offsetTime.setDate(offsetTime.getDate() - offsetTime.getDay() + 1)
);
const lastDay = new Date(
offsetTime.setDate(offsetTime.getDate() - offsetTime.getDay() + 7)
);
setWeek({ firstDay, lastDay });
const value = e.target.value;
setState({
...state,
[e.target.name]: value,
startdate: convertDate(firstDay),
enddate: convertDate(lastDay)
})
};
const renderDays = () => {
let month = date.getMonth() + 1;
let ar = [];
for (let i = 1; i <= days[month]; i++) {
let currentDate = new Date(date).setDate(i);
let cName = "single-number ";
if (
new Date(state.startdate).getTime() <= new Date(currentDate).getTime() &&
new Date(currentDate).getTime() <= new Date(state.enddate).getTime()
) {
cName = cName + "selected-week";4
console.clear();
console.log(new Date().getTimezoneOffset());
console.log("start date: ", new Date(state.startdate).getTime());
console.log("converted start date: ", convertDate(new Date(state.startdate).getTime()));
console.log("current date: ", new Date(currentDate).getTime());
console.log("converted current date: ", convertDate(new Date(currentDate).getTime()));
console.log("end date: ", new Date(state.enddate).getTime());
console.log("converted end date: ", convertDate(new Date(state.enddate).getTime()));
}
ar.push(
<div key={v4()} id={i} className={cName} onClick={handleClick}>
{i}
</div>
);
}
const displayDate = new Date(date).setDate(1);
let dayInTheWeek = new Date(displayDate).getDay();
let empty = [];
for (let i = 1; i < dayInTheWeek; i++) {
empty.push(<div key={v4()} id={i} className="single-number empty"></div>);
}
return [...empty, ...ar];
};
Here's a pic of what I am console.logging so you can see that I am capturing the intended dates (ignore the undefined). This is the state being console logged in the useEffect()
But also check out the individual logs from within the renderDays()
. They're a day behind
I just need the days in the picker to + 1 in the view Thanks in advanced you for your help