I’m trying to go next and prev with with days that have a format of ‘2022-11-5’
I get an error when trying to get the getDate I think im changing the values to Unix time or something. full code in codesanbox
switch (move) {
case "next":
const y = dia;
const n = y.setDate(y.getDate() + 1);
return setDia(n);
case "prev":
newDate = dia.setDate(dia.getDate() - 1);
return setDia(newDate);
default:
throw console.log("error");
}
this was a pain in the ass got it working finally
const formatDay = (date) => new Date(date.replace(/[-]/g,'/'))
function useNextDate(stockDate = '2022-02-26', stocks = []){
const [dia, setDia] = useState( formatDay(stockDate))
const startDate = formatDay( stocks[0].date )
const endDate = formatDay( stocks[stocks.length-1].date)
const checkDay = (move) => {
switch(move){
case 'next':
const n = dia.setDate(dia.getDate() + 1)
return setDia( new Date(n));
case 'prev':
const p = dia.setDate(dia.getDate() -1)
return setDia( new Date(p));
default:
throw console.log('error')
}
}
const prev = useCallback(() => {
if ( (dia - startDate) === 0 ) return setDia(endDate);
if( dia.getDay() === 5 ) {
const r = dia.setDate(dia.getDate() + 3)
setDia( new Date(r));
} else {
checkDay('prev')
}
}, [dia]);
const next = useCallback(() => {
if ( (dia - endDate) === 0) return setDia(startDate);
if( dia.getDay() === 5 ) {
const r = dia.setDate(dia.getDate() + 3)
setDia( new Date(r));
} else {
checkDay('next')
}
}, [dia]);
console.log('next',dia, startDate, endDate, dia - startDate,dia - endDate )
// console.log(dia.getDay(), dia.getDay() === 6, dia, stocks)
return [dia, prev, next]
}
system
Closed
September 15, 2022, 8:25am
3
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.