0

I created one calendar and I don't find the solution to add one className when the date is equal "today", please see the link demo:

https://compassionate-beaver-fe6c05.netlify.com/

the code is:

https://github.com/fzabra/react-calendar/blob/master/src/Components/Calendar.jsx

The follow condition return the date today:

 if (dateDay === today.getDate() && currentYear === today.getFullYear() && currentMonth === today.getMonth()) {
   console.log(dateDay)   
}

Observe that the console show the date of today. The square today is a it element have one id="dateDay", I think something like document.getElementById doesn't work, anybody has any ideas?

Thank you

4
  • Where is the style for the current date? Commented May 31, 2019 at 1:26
  • 'Conditional Rendering' Commented May 31, 2019 at 1:27
  • Yes, but there's no css style, dateDay is a number, add a today or something Commented May 31, 2019 at 1:32
  • I don't create the style yet, I just want to add one className in the current day. I tried push() again between this conditional but it is creating two current day Commented May 31, 2019 at 1:44

1 Answer 1

1

Using the .box .green class from your CSS file, you can push the <Table.Cell> inside the if condition or else don't use the className, like so:

if (dateDay === today.getDate() && currentYear === today.getFullYear() && currentMonth === today.getMonth()) {
  children.push(<Table.Cell key={j} id={dateDay} className="box green">{dateDay}</Table.Cell>)
  } else{
  children.push(<Table.Cell key={j} id={dateDay} className="box">{dateDay}</Table.Cell>)

This way the current date will have a green background.
Result
enter image description here

Full code

Sign up to request clarification or add additional context in comments.

2 Comments

For me it created 2 current days not for you
Very nice, now I will understand where I broke my brain haha, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.