0

I need to display a component only during business hours.

Right now, my plan is to create an object called Hours that has weekday:[openTime, closeTime] as key/value pairs for each day of the week.

Then, I am planning on using JavaScript to get the current time, then using that to get the weekday. I will see if the current time is between the times I will fetch from the previously created object (Hours.weekday), and render the component if true.

This seems very overly complicated, but it is the only way I could think of. I am still pretty new to JavaScript and React, so if you have any tips or more concise ways to do this, please let me know!

const hours = new Object();
hours.monday = [9, 5]
hours.tuesday = [9, 5]
hours.wednesday = [9, 5]
hours.thursday = [9, 5]
hours.friday = [9, 5]
hours.saturday = [10, 4]
hours.sunday=[null, null]
chazsolo
  • 7,429
  • 1
  • 18
  • 42
  • 1
    You may want to [brush up on the Date object and it's methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). Think about how to represent day of the week and hours in terms that the API can handle (e.g. [9, 5] makes sense to us, but a computer only sees two numbers, not "9 am" and "5 pm"). You're thought process isn't overly complicated (imo), but it'd be good if you could try to work through some of those details first then come back when you get stuck. – chazsolo Aug 30 '21 at 19:44

0 Answers0