Edit Note: This referred to question does not actually solve my first problem, it was a matter of an extra {} around first ternary result. I replaced them with parenthesis. Josep's solution using getTime I ended up needing to equate the dates. the Date().setHours(0,0,0,0) I used on the real time date to equate to the user generated date which has 0 as hours, minutes, seconds, then .getTime()
Why can't hour
access the const hour = this.state.date.getHours()
of (state date = new Date()) inside constructor of the component, when I put it in the date === ternary? the hour nested ternary gets hour when on it's own in the jsx of react component. new Date(this.state.year, this.state.month, this.state.day)
is used as the day selected by the user that sets state when the user selects it - so if it equals the current date
, I want to render the image clockbordermoon
or clockbordersun
in the nested ternary.
I've tried a nested ternary inside an if statement's render but still couldn't reach hour
{date === new Date(this.state.year, this.state.month, this.state.day) ?
{hour === 20 ||
hour === 21 ||
hour === 22 ||
hour === 23 ||
hour === 0 ||
hour === 1 ||
hour === 2 ||
hour === 3 ||
hour === 4 ? (
<img
src={clockbordermoon}
className="clockborder"
style={{
transform: `translate(-50%, -50%)rotate(${totaldegrees}deg)`
}}
alt="error"
/>
) : (
<img
src={clockbordersun}
className="clockborder"
style={{
transform: `translate(-50%, -50%)rotate(${totaldegrees}deg)`
}}
alt="error"
/>
)}
: null}
how can I get hour
to read const hour = this.state.date.getHours()
, which is inside render before the divs & imgs?
this is my second question today, I should be set for a while after this one, sorry
full code (tentatively): https://codesandbox.io/s/zen-dijkstra-1c31n?fontsize=14 this component is src>UIConainers>Calendar>CalSlideDrawer.js, found by top-left logo icon (after pressing the inbox icon if you're starting on the purple screen)
Edit (error Unexpected token, expected "," after first hour was because I didn't need the brackets around the first result of the ternary):
class CalSlideDrawer extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date(),
day: new Date().getDate(),
month: +new Date().getMonth() + 1,
year: +new Date().getFullYear(),
};
}
//statefor
const monthname = Object.keys(CALENDAR_MONTHS)[
Math.max(0, Math.min(month - 1, 11))
].toUpperCase();
const weekdayname = new Date(this.state.year, this.state.month, this.state.day);
const hour = this.state.date.getHours();
return (
{date.getTime() === weekdayname.getTime() ?
(hour === 20 ||
hour === 21 ||
hour === 22 ||
hour === 23 ||
hour === 0 ||
hour === 1 ||
hour === 2 ||
hour === 3 ||
hour === 4 ? (
<img
src={clockbordermoon}
/>
) : (
<img
src={clockbordersun}
/>
)): null}